Henry
Henry

Reputation: 171

Error in MATLAB: first input subs must contain positive integer

Is there any operation in MATLAB that removes zeros from number

150.0000  150.0000    0.0000
  143.0000  148.0000    0.0000
  152.0000  152.0000    0.0000
  152.0000  144.0000    0.0000
  146.0000  150.0000    0.0000
  149.0000  145.0000    0.0000
  148.0000  152.0000    0.0000
  152.0000  157.0000    0.0000
  157.0000  164.0000    0.0000
  160.0000  155.0000    0.0000
  154.0000  154.0000    0.0000

The reason I ask is because I want to use them in function called accumarray and I keep getting the error

First input SUBS must contain positive integer subscripts.

Thanks

Update: Eventually I would like to use as follows

[X] = round(x)
[Y] = round(y)
data = [X,Y,Z];
plotdata = accumarray(data(:,1:2),data(:,3));
surface(plotdata)
colorbar

Upvotes: 0

Views: 1942

Answers (1)

Adriaan
Adriaan

Reputation: 18177

[X] = round(x-min(x))+1;
[Y] = round(y-min(y))+1;
data = [X,Y,Z];
plotdata = accumarray(data(:,1:2),data(:,3));
surface(unique(x),unique(y),plotdata)
colorbar

min() moves your array to the upper-left corner of your matrix, basically removing leading zero rows and columns. The +1 ensures it does not end up at zero, which gives the error in accumarray.

Upvotes: 1

Related Questions