find neighbours of neighbours

1 回表示 (過去 30 日間)
em
em 2015 年 3 月 3 日
コメント済み: em 2015 年 3 月 4 日
I have a matrix which represents a neighbourhood relationship of index
A=[1 2
1 4
2 6
4 5
6 7
6 8]
A is in ascending order of A(:,1) and not including duplicate neighbours, meaning [1 2] is [2 1] is considered as the same neighbourhood relationship.
In matrix A, it means index 1 is the neighbour of 2 and 4, 2 is the the neighbour of 6, and 4 is the neighbour of 5. I want to compute a matrix B that represents the neighbours of neighbour(NON) relationship. This means 1 is the NON of 5 and 6, etc.
B=[1 5
1 6
2 7
2 8]
How can I compute such B

採用された回答

Matt J
Matt J 2015 年 3 月 3 日
編集済み: Matt J 2015 年 3 月 3 日
I think the code below would do it, but I think your example is missing some NONs. If, as you say, [1,2] and [2,1] are equivalent, then I think 2 and 4 should be NONs because they are both neighbors of 1. Similarly, 7 and 8 both neighbor 6.
m=max(A(:));
Ac=sparse(A(:,1),A(:,2),true,m,m);
Ac=double(Ac|Ac.');
Bc=(Ac*Ac)&(~Ac);
[i,j]=find(tril(Bc,-1));
B=[j,i]
  3 件のコメント
Matt J
Matt J 2015 年 3 月 3 日
編集済み: Matt J 2015 年 3 月 3 日
Yep. My code is consistent with that. Nodes 2 and 4 are two layers apart, as are nodes 7 and 8.
em
em 2015 年 3 月 4 日
This works! Thanks alot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by