find 2 common values of 3 in row matrix?

HI,
I have a 3 columns and n rows matrix I need to find in this matrix, witch rows own the same 2 values of the 3 as the first row. the values can be in a other column order. then find witch rows own the same 2 values of the 3 as the second row...etc
example: A=[1,2,3;3,2,4;5,6,7;7,6,8]
the first loop must find that row 1 has 2 commun values with row 2. third loop must find that row 3 has 2 commun values with row 4.

 Respuesta aceptada

dpb
dpb el 30 de En. de 2015
>> for i=1:2:size(A,1),cmn=intersect(A(i,:),A(i+1,:)),end
cmn =
2 3
cmn =
6 7
>>

1 comentario

Stephen23
Stephen23 el 31 de En. de 2015
Note that i and j should not be used for variable names, as these are both names of the inbuilt imaginary unit .

Iniciar sesión para comentar.

Más respuestas (1)

nicolas
nicolas el 2 de Feb. de 2015
Editada: dpb el 2 de Feb. de 2015
I have [written] this code, it works, but it is very slow, because of the two if loops. Is it possible to optimized it?
for i=1:size(Lien,1)
k=1;
for j=1:size(Lien,1)
sommetscommuns=intersect(Lien(i,:),Lien(j,:))
if size(sommetscommuns,2)==2
voisins(i,k)=j;
k=k+1;
end
end
if voisins(i,1) ~=0 && voisins(i,2) ~=0 && voisins(i,3) ~=0
Qnor(i,1)=(dot(Normales(i,:),Normales(voisins(i,1),:)) + ...
dot(Normales(i,:),Normales(voisins(i,2),:)) + ...
dot(Normales(i,:),Normales(voisins(i,3),:)))/3;
else
Qnor(i,1)=10;
end
end
th[a]nks of lot

1 comentario

1) Have you preallocated Qnor? If not, that'll help noticeably if sizes are very large
2) Is this not symmetric in the end? If so, running the loop only over the triangular section would cut it roughly in half. Also, the i==j locations are all identities.
Just one minor syntax that won't have any significant impact on speed would be
if voisins(i,1) ~=0 && voisins(i,2) ~=0 && voisins(i,3) ~=0
can be written as
if all(voisins(i,:))

Iniciar sesión para comentar.

Categorías

Preguntada:

el 30 de En. de 2015

Comentada:

dpb
el 2 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by