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.

1 件のコメント

Image Analyst
Image Analyst 2015 年 1 月 30 日
Are these guaranteed to be integers , or can they be floating point numbers?

サインインしてコメントする。

 採用された回答

dpb
dpb 2015 年 1 月 30 日

0 投票

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

1 件のコメント

Stephen23
Stephen23 2015 年 1 月 31 日
Note that i and j should not be used for variable names, as these are both names of the inbuilt imaginary unit .

サインインしてコメントする。

その他の回答 (1 件)

nicolas
nicolas 2015 年 2 月 2 日
編集済み: dpb 2015 年 2 月 2 日

0 投票

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 件のコメント

dpb
dpb 2015 年 2 月 2 日
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,:))

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2015 年 1 月 30 日

コメント済み:

dpb
2015 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by