please help>>>how to fix (Matrix dimensions must agree.) error in matlab?

how to fix (Matrix dimensions must agree.) error in matlab?
my code is:
a=imread('lena.bmp');
I=im2double(a);
H=fspecial('log',5);
%convolution
[r,c]=size(I);
for y=2:r-1
for x=2 :c-1
windowI= I(y:y+3 , x: x+3);
windowH= H(y:y+1 , x: x+1);
IH= sum(sum(windowI .* windowH));
end
end
figure(2);
imshow (IH) ;
i think this line: IH= sum(sum(windowI .* windowH)); causes the error but how to fix it?

Antworten (3)

Walter Roberson
Walter Roberson am 14 Nov. 2013

0 Stimmen

Why are you expecting that a matrix that is 4 x 4 can be multiplied by a matrix that is 2 x 2 ?

4 Kommentare

Asma
Asma am 14 Nov. 2013
Bearbeitet: Asma am 15 Nov. 2013
i am not expecting that , but i don't know how to fix it. i tried windowH= H(y:y+3 , x: x+3); and it didn't work too. I do't understand how to know the matrix size from the code, i don't know what is +3 in (y:y+3 , x: x+3) or what is -1 in (y=2:r-1) so how to relate between H which is 5*5 and my image which is 215*215? i thought about creating a window to pass on the image, i understand that dimensions must agree but i don't know how to code it
What is the intent? To tile the 2 x 2 into a 4 x 4 and then multiply?
Perhaps you should be using conv2() ?
Asma
Asma am 15 Nov. 2013
Bearbeitet: Asma am 15 Nov. 2013
what i am trying to do is coding conv2(I,H), so i can't use it . my problem is that I and H are of different sizes and i don't know how to do the for loops :(

Melden Sie sich an, um zu kommentieren.

Youssef  Khmou
Youssef Khmou am 15 Nov. 2013
Bearbeitet: Youssef Khmou am 15 Nov. 2013
hi Asma,
the number of columns of WindowI must be the same as the number of lines in WindowH, take the two blocks with same dimensions or try :
a=imread('circuit.tif');
I=im2double(a);
H=fspecial('log',5);
[r,c]=size(I);
a=1;b=1;
for x=1:r-5 % ver
for y=1:c-4
windowI= I(x:x+4 , y: y+4);
%windowH= H(x:x+3 , y: y+1);
IH(x,y)= sum(sum(windowI .* H)); % element wise or matrix product????????
end
end
figure(2); imshow (IH)

2 Kommentare

Asma
Asma am 15 Nov. 2013
Bearbeitet: Asma am 15 Nov. 2013
element wise. can you please tell me what these numbers mean? +3 or +1 in (windowH= H(x:x+3 , y: y+1); and why do we need to start the loop from x=2? and what does r-3 mean? i am sorry to ask too many silly questions but i am new to coding. now i tried the code below but i got this error: ??? Error using ==> unknown Matrix dimensions must agree.
Error in ==> log_tryy at 11 IH= sum(sum(windowI .* windowH)); % element wise
the code is: a=imread('lena.bmp'); I=im2double(a); H2=fspecial('log',5); H1=flipud(H2); H=fliplr(H1); [r,c]=size(I); for x=2:r-3 for y=2 :c-3 windowI= I(x:x+3 , y: y+3); windowH= H(x:x+3 , y: y+1); IH= sum(sum(windowI .* windowH)); % element wise end end figure(2); imshow (IH)
what should i do to make both windows of size 5*5 for examlpe?

Melden Sie sich an, um zu kommentieren.

Asma
Asma am 15 Nov. 2013

0 Stimmen

what should i do to make both windows of size 5*5 for examlpe?

1 Kommentar

the size of windowI depends on H, as you fspecal is 5x5 then a window must have the size 5*m , for arbitrary m, try the example i posted

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 14 Nov. 2013

Kommentiert:

am 15 Nov. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by