how to compare a line a of matrix with another ?

hello, giving the following matrix :
matrix=[5 6 4;3 4 4;5 6 4;7 8 4;9 10 4;1 2 4];
I want to compare the first 2 elements of each rows and display a message if 2(or more) of them have the same elements
I tried to do it this way:
matrix=[5 6 4;3 4 4;5 6 4;7 8 4;9 10 4;1 2 4];
for i=1:size(matrix,1)
a=matrix(i,1);
for k=1:size(matrix,2)
b=matrix(k,2);
m1=[a b];
end
end
for i=1:size(matrix,1)
c=matrix(i,1);
for k=1:size(matrix,2)
d=matrix(k,2);
m2 =[c d];
end
end
if m2==m1
disp('YOU GOT IT !!');
end
matrix
I created 2 vectors m1 and m2 with the values of the first elements and then compare the vectors.
but I dont get any positve results.

 Akzeptierte Antwort

dpb
dpb am 17 Jun. 2015

0 Stimmen

>> if size(unique(matrix(:,1:2),'rows'),1)<size(matrix,1)
disp('Is at least one match')
else
disp('All unique')
end
Is match
>>

5 Kommentare

Paul Nanfah
Paul Nanfah am 18 Jun. 2015
Bearbeitet: Paul Nanfah am 18 Jun. 2015
thanks,
I tried without the command match and it seems to be useless. or am I missing something here ?
thank you again.
Paul Nanfah
Paul Nanfah am 18 Jun. 2015
how can I extend the commands so that e.g disp('Similar numbers') is displayed when the 2 elements are the same , and even if there are not in the same oder.
dpb
dpb am 18 Jun. 2015
Bearbeitet: dpb am 18 Jun. 2015
I don't understand the first comment at all and the order is immaterial in the above.
Your example above only makes an output display of whether the two are the same; that's what the above code snippet does, also, but demonstrates at least one way in which to do the comparison to ascertain if that is or is not true. Consequently, I returned nothing, simply used the result of the logic test to make the decision branch.
If you have some other needs regarding what are/are not the specifics about the content, need to
  1. define those requirements, and
  2. incorporate the code to do so.
The latter would likely entail making use of one or more of the outputs available from unique instead of simply counting the returned number of unique elements as here.
Paul Nanfah
Paul Nanfah am 22 Jun. 2015
Bearbeitet: Paul Nanfah am 22 Jun. 2015
sry that the first command was not clear, I actualy meant the command "Is match" at the end of your code seems to be of no use, unless I am wrong.
dpb
dpb am 22 Jun. 2015
Bearbeitet: dpb am 23 Jun. 2015
Oh, I see...actually, that's output from the command line, not a command at all (as is shown by it's not preceded by the prompt >> characters and isn't part of a continuing code block as the previous lines from if to the end). I initially had only that string as the disp argument for the match; I expanded it when pasting in the Answer by inserting the "at least one" phrase to make the resulting message more explicit but didn't do the same with the previous output so the two don't match...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 17 Jun. 2015

Bearbeitet:

dpb
am 23 Jun. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by