how to compare a line a of matrix with another ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Paul Nanfah
el 17 de Jun. de 2015
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.
0 comentarios
Respuesta aceptada
dpb
el 17 de Jun. de 2015
>> 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 comentarios
dpb
el 22 de Jun. de 2015
Editada: dpb
el 23 de Jun. de 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...
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!