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
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 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일
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)
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

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 Exchange에서 Resizing 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