Matrix manipulation

Hi I'm new to Matlab and seem to be struggling again. I'm trying to subtract corresponding elements from two image matrices i.e.
matrix 1= A B matrix 2= a b C D c d
I'm trying to get (A-a)^2+(B-b)^2.....
Thanks in advance for any help, it;s really appreciated.

Antworten (4)

the cyclist
the cyclist am 25 Mär. 2011

1 Stimme

Not 100% if this is what you mean, but ...
M1 = [1 2 3 4];
M2 = [5 6 7 8];
M3 = (M1-M2).^2;

5 Kommentare

Karen Rafferty
Karen Rafferty am 25 Mär. 2011
Yeah that seems like it. I'm trying to do it for two 568 by 752 matrices. From the above I'd be doing M3=(1-5)^2 + (2-6)^2 + (3-7)^2+ (4-8)^2
Is it possible to do so? I'm guessing I'd need a for loop.
Thanks
Matt Fig
Matt Fig am 25 Mär. 2011
No FOR loop is needed.
M3 = sum((M1-M2).^2)
If you expect a scalar from the operation, do this:
M3 = sum((M1(:)-M2(:)).^2)
Karen Rafferty
Karen Rafferty am 25 Mär. 2011
I tried this: M3 = sum((Groundtruth-Haarbk2).^2)
but got the error message:
??? Error using ==> minus
Class of operand is not supported.
Matt Fig
Matt Fig am 25 Mär. 2011
Yes, what version are you using, because this works find on my system.
Try this:
sum((Groundtruth-double(Haarbk2)).^2)
Karen Rafferty
Karen Rafferty am 25 Mär. 2011
Thanks that seems to be working now

Melden Sie sich an, um zu kommentieren.

Sean de Wolski
Sean de Wolski am 25 Mär. 2011

0 Stimmen

M3 = sum((Groundtruth(:)-double(Haarbk2(:))).^2);
Karen Rafferty
Karen Rafferty am 25 Mär. 2011

0 Stimmen

I've tried to implement these by: for i = 1:M for j = 1:N Ans= sum(abs(Groundtruth(i,:)-Haarbk2(i,:))).^2; end end
But I keep getting the error message:
??? Error using ==> minus Class of operand is not supported.

3 Kommentare

Matt Fig
Matt Fig am 25 Mär. 2011
So what does this return:
class(Groundtruth)
class(Haarbk2)
Karen Rafferty
Karen Rafferty am 25 Mär. 2011
They give:
>> class(Groundtruth)
ans =double
>> class(Haarbk2)
ans =uint8
Should they be the same?
Sean de Wolski
Sean de Wolski am 25 Mär. 2011
Yes. You can't have a negative unsigned integer.
Haarbk2 = double(Haarbk2);

Melden Sie sich an, um zu kommentieren.

Karen Rafferty
Karen Rafferty am 25 Mär. 2011

0 Stimmen

Thank you soo much for all your help, I think I've it sorted now :D

Gefragt:

am 25 Mär. 2011

Community Treasure Hunt

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

Start Hunting!

Translated by