Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compare two matrices row by row

Subject: Compare two matrices row by row

From: Jess J

Date: 10 Mar, 2009 14:51:02

Message: 1 of 5

I ve two matrices Y and T, I wanna compare this two matrices row by row.
If row i of Y and row i of T is the same, then show "Correct", else show "Incorrect".
However i ve some problem here, please help me.
Thanks.

For Example,
Y = [1101; 1111; 0101]
T = [1101; 1110; 0101]

output will be :
Correct
Incorrect
Correct

Below is my coding:

for i = i+1;
    if Y(i,:)==T(i,:);
     disp('Correct',i);
    elseif Y(i,:)~=T(i,:);
       disp('Incorrect',i);
    end
end
     
Best Regards,
Jess

Subject: Compare two matrices row by row

From: Eric Johnson

Date: 10 Mar, 2009 15:12:05

Message: 2 of 5

Jess,

You can simply use

   all(T == Y,2)


-Eric


"Jess J" <jessie_jtl@yahoo.co.uk> wrote in message
news:gp5uom$r4i$1@fred.mathworks.com...
>I ve two matrices Y and T, I wanna compare this two matrices row by row.
> If row i of Y and row i of T is the same, then show "Correct", else show
> "Incorrect".
> However i ve some problem here, please help me.
> Thanks.
>
> For Example,
> Y = [1101; 1111; 0101]
> T = [1101; 1110; 0101]
>
> output will be :
> Correct
> Incorrect
> Correct
>
> Below is my coding:
>
> for i = i+1;
> if Y(i,:)==T(i,:);
> disp('Correct',i);
> elseif Y(i,:)~=T(i,:);
> disp('Incorrect',i);
> end
> end
>
> Best Regards,
> Jess

Subject: Compare two matrices row by row

From: Jess J

Date: 10 Mar, 2009 15:46:02

Message: 3 of 5

"Eric Johnson" <eric.johnson@mathworks.com> wrote in message <gp6005$l42$1@fred.mathworks.com>...
> Jess,
>
> You can simply use
>
> all(T == Y,2)
>
>
> -Eric
>
>
> "Jess J" <jessie_jtl@yahoo.co.uk> wrote in message
> news:gp5uom$r4i$1@fred.mathworks.com...
> >I ve two matrices Y and T, I wanna compare this two matrices row by row.
> > If row i of Y and row i of T is the same, then show "Correct", else show
> > "Incorrect".
> > However i ve some problem here, please help me.
> > Thanks.
> >
> > For Example,
> > Y = [1101; 1111; 0101]
> > T = [1101; 1110; 0101]
> >
> > output will be :
> > Correct
> > Incorrect
> > Correct
> >
> > Below is my coding:
> >
> > for i = i+1;
> > if Y(i,:)==T(i,:);
> > disp('Correct',i);
> > elseif Y(i,:)~=T(i,:);
> > disp('Incorrect',i);
> > end
> > end
> >
> > Best Regards,
> > Jess
>
********************************************************
erm... it doesn't work...
i need to compare them row by row...
not the whole cell array...
Any other suggestions?

Best Regards,
Jess

Subject: Compare two matrices row by row

From: Bruno Luong

Date: 10 Mar, 2009 16:05:18

Message: 4 of 5

"Jess J" <jessie_jtl@yahoo.co.uk> wrote in message <gp61vq$7ak$1@fred.mathworks.com>...

> erm... it doesn't work...
> i need to compare them row by row...
> not the whole cell array...

Eric's solution compares row by row in fact in one shot. That's the beauty of Matlab.

all(T == Y,2) returns the result that has the same number of rows, and equals 1 (true) when two rows are equals, 0 otherwise.

Try this:

T = [1101; 1110; 0101];
Y = [1101; 1111; 0101];

Message={'Incorrect' 'Correct'}';
Message(all(T == Y,2)+1)

% Bruno

Subject: Compare two matrices row by row

From: Adrian Cherry

Date: 10 Mar, 2009 15:58:53

Message: 5 of 5

"Jess J" <jessie_jtl@yahoo.co.uk> wrote in
news:gp61vq$7ak$1@fred.mathworks.com:

> "Eric Johnson" <eric.johnson@mathworks.com> wrote in message
> <gp6005$l42$1@fred.mathworks.com>...
>> Jess,
>>
>> You can simply use
>>
>> all(T == Y,2)
>>
>>
>> -Eric
>>
>>
>> "Jess J" <jessie_jtl@yahoo.co.uk> wrote in message
>> news:gp5uom$r4i$1@fred.mathworks.com...
>> >I ve two matrices Y and T, I wanna compare this two
>> >matrices row by row.
>> > If row i of Y and row i of T is the same, then show
>> > "Correct", else show "Incorrect".
>> > However i ve some problem here, please help me.
>> > Thanks.
>> >
>> > For Example,
>> > Y = [1101; 1111; 0101]
>> > T = [1101; 1110; 0101]
>> >
>> > output will be :
>> > Correct
>> > Incorrect
>> > Correct
>> >
>> > Below is my coding:
>> >
>> > for i = i+1;
>> > if Y(i,:)==T(i,:);
>> > disp('Correct',i);
>> > elseif Y(i,:)~=T(i,:);
>> > disp('Incorrect',i);
>> > end
>> > end
>> >
>> > Best Regards,
>> > Jess
>>
> ********************************************************
> erm... it doesn't work...
> i need to compare them row by row...
> not the whole cell array...
> Any other suggestions?
>
> Best Regards,
> Jess
>

erm... in what way doesn't it work? What result do you get?

because when I try all(T==Y,2) I get a 3x1 logical output

  1
  0
  1

Which as far as I'm concerned is equivalent to your desired
output.

Adrian

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us