I'm getting an error using: ver_4/singleButtonPushed The index in position 1 is invalid. Array indices must be positive integers or logical values.

1 回表示 (過去 30 日間)
function [vector] = SUBPIX2DGAUSS (result_conv,interrogationarea,x,y,SubPixOffset)
if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
c10=zeros(3,3);
c01=c10;
c11=c10;
c20=c10;
c02=c10;
for i=-1:1
for j=-1:1
if i ~= 0
c10(j+2,i+2)=i*log(result_conv(y+j, x+i)); % It says that the index is invalid in this part
c11(j+2,i+2)=i*j*log(result_conv(y+j, x+i));
end
if j~=0
c01(j+2,i+2)=j*log(result_conv(y+j, x+i));
end
c20(j+2,i+2)=(3*i^2-2)*log(result_conv(y+j, x+i));
c02(j+2,i+2)=(3*j^2-2)*log(result_conv(y+j, x+i));
end
end
c10=(1/6)*sum(sum(c10));
c01=(1/6)*sum(sum(c01));
c11=(1/4)*sum(sum(c11));
c20=(1/6)*sum(sum(c20));
c02=(1/6)*sum(sum(c02));
temp=4*c20*c02-c11^2;
deltax=(c11*c01-2*c10*c02)/temp;
deltay=(c11*c10-2*c01*c20)/temp;
peakx=x+deltax;
peaky=y+deltay;
SubpixelX=peakx-SubPixOffset;
SubpixelY=peaky-SubPixOffset;
vector=[SubpixelX, SubpixelY];
else
vector=[NaN NaN];
end
end

採用された回答

Voss
Voss 2022 年 5 月 27 日
編集済み: Voss 2022 年 5 月 27 日
Suppose result_conv is of size 2-by-2 and x is 1 and y is 1. Then all the if conditions are met, so the for loops are executed. Then the first iteration of the inner loop has i=-1 and j=-1, so y+j=0 and x+i=0. That means the code will try to access result_conv(0,0), which is in error because indexing starts with 1 in MATLAB.
% Suppose result_conv is of size 2-by-2 and x is 1 and y is 1
if (x <= (size(result_conv,1)-1)) ... % 1 <= 1
&& (y <= (size(result_conv,1)-1)) ... % 1 <= 1
&& (x >= 1) ... % 1 >= 1
&& (y >= 1) % 1 >= 1
% ...
for i=-1:1
for j=-1:1
if i ~= 0
% the first time here, i=-1 and j=-1, so
% y+j=0 and x+i=0, so result_conv(y+j,x+i) is
% result_conv(0,0), an error
c10(j+2,i+2)=i*log(result_conv(y+j, x+i));
% ...
end
% ...
end
end
end
Perhaps the 3rd and 4th conditions in the if statement should be x >= 2 && y >= 2?
Or equivalently, if x and y are always integers (which they should be since they're being used for indexing), perhaps this is more clear:
if x < size(result_conv,1) ...
&& y < size(result_conv,1) ...
&& x > 1 ...
&& y > 1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by