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.

3 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Voss
Voss am 27 Mai 2022
Bearbeitet: Voss am 27 Mai 2022
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by