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?

回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 11 月 14 日

0 投票

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

4 件のコメント

Asma
Asma 2013 年 11 月 14 日
編集済み: Asma 2013 年 11 月 15 日
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
Walter Roberson
Walter Roberson 2013 年 11 月 14 日
What is the intent? To tile the 2 x 2 into a 4 x 4 and then multiply?
Walter Roberson
Walter Roberson 2013 年 11 月 15 日
Perhaps you should be using conv2() ?
Asma
Asma 2013 年 11 月 15 日
編集済み: Asma 2013 年 11 月 15 日
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 :(

サインインしてコメントする。

Youssef  Khmou
Youssef Khmou 2013 年 11 月 15 日
編集済み: Youssef Khmou 2013 年 11 月 15 日

0 投票

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 件のコメント

Asma
Asma 2013 年 11 月 15 日
編集済み: Asma 2013 年 11 月 15 日
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)
Asma
Asma 2013 年 11 月 15 日
what should i do to make both windows of size 5*5 for examlpe?

サインインしてコメントする。

Asma
Asma 2013 年 11 月 15 日

0 投票

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

1 件のコメント

Youssef  Khmou
Youssef Khmou 2013 年 11 月 15 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2013 年 11 月 14 日

コメント済み:

2013 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by