Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum(Array) says index exceeds matrix dimensions [duplicate]

Tags:

matlab


I am a newbie to Matlab .I was trying to do some image matching.Image X is bigger and Image Y is smaller.Our aim is to find all possible overlaps of y with x and then compute sum of square differences for RGB at each such overlap(ie x^2+y^2+z^2).So I find a location (i,j) in bigger image from where we pick a tile and match . To be precise here is the code :-

a=size(X,1)-size(Y,1);
b=size(X,2)-size(Y,2);
Z=ones(a,b);

for i=1:a
for j=1:b 
    A=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,1)-Y(1:end,1:end,1)].^2;
    B=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,2)-Y(1:end,1:end,2)].^2;
    C=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,3)-Y(1:end,1:end,3)].^2;           
    T=A+B+C
    size(T)
    P=sum(sum(T))
    %Z(i,j)=sum(sum(T));
end;    
end;    

Now the problem is the line sum(sum(T)) throws up an error-'Index exceeds Matrix Dimensions'.Can someone help?My aim is to add all these differences and put it in a matrix Z.

like image 482
Manish Avatar asked Dec 04 '22 10:12

Manish


1 Answers

The only way you can get such a bug is if you have declared a variable sum somewhere.

Run clear sum (or clear all, or restart Matlab) and try again, then everything should work fine.

like image 194
Jonas Avatar answered Dec 29 '22 09:12

Jonas