sub2ind error: Out of range?

조회 수: 3 (최근 30일)
M W
M W 2020년 1월 30일
댓글: Adam 2020년 1월 30일
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
Walter Roberson
Walter Roberson 2020년 1월 30일
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 2020년 1월 30일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by