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.

 Accepted Answer

>> for i=1:2:size(A,1),cmn=intersect(A(i,:),A(i+1,:)),end
cmn =
2 3
cmn =
6 7
>>

1 Comment

Note that i and j should not be used for variable names, as these are both names of the inbuilt imaginary unit .

Sign in to comment.

More Answers (1)

nicolas
nicolas on 2 Feb 2015
Edited: dpb on 2 Feb 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 Comment

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,:))

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 30 Jan 2015

Commented:

dpb
on 2 Feb 2015

Community Treasure Hunt

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

Start Hunting!