sub2ind error: Out of range?

3 views (last 30 days)
M W
M W on 30 Jan 2020
Commented: Adam on 30 Jan 2020
I don't understand the following error:
Error using sub2ind (line 73)
Out of range subscript.
The code
[R,C,Z] = size(imc);
[R,C] = meshgrid(1:1:R,1:1:C);
Z=Zf(R); %function
Z=floor(Z);
ind = sub2ind(size(image),R,C,Z)
Size(image)= 352 229 277
size(R )=352 229
size(C )=352 229
size(Z)=352 229
I use this line of coding for different images. In some images I get the error and in others not.
The size of the image, R, C and Z is the same in all the images, so I don't understand why the error is 'Out of range subscript'.
Has it something to do with the values of R, C and Z?
The values of R and C are constant for all images.
The Z values are dependent of the function Zf.
I tried this to keep the values of Z within the size of the images:
domein=size(imc);
domein_Z=domein(1,3);
for ii = 1:numel(Z)
if (Z(ii) > domein_Z | Z(ii) < 0)
Z(ii) = 50;
end
end
This doesn't make a difference.
  2 Comments
Walter Roberson
Walter Roberson on 30 Jan 2020
Yes it is the values of those that is the problem. Each must be no larger in value than the size of the corresponding dimension.
Adam
Adam on 30 Jan 2020
Yes, the size of R, C and Z is irrelevant (though they do have to be the same size as each other, of course). It is the values inside them that are being interpreted as subscripts.

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!