find an element in a pair and return the counterpart

1 回表示 (過去 30 日間)
congcong
congcong 2015 年 8 月 19 日
コメント済み: congcong 2015 年 8 月 19 日
I have a pair matrix: for example node_pair_list=[ [1, 2];[2, 3]; [4, 3]; [3, 6]; [5, 6] ]. Note: each pair is unique, [3, 4] is equivalent to [4, 3] but this pair will only appear once. Now I have a single element 3 (current_node) for example, I want to check whether 3 contains in any of these pairs. And then find the corresponding element (counterpart)paired with 3. For example in this case, the pair found is [2, 3], [4, 3], [3, 6] and the counterpart is 2, 4 and 6. How can I do this easily, vectorizely? Thank you very much!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 19 日
node_pair_list= [1, 2;2, 3; 4, 3; 3, 6; 5, 6 ]
idx=ismember(node_pair_list,3)
out=node_pair_list(any(idx,2),:)
  2 件のコメント
congcong
congcong 2015 年 8 月 19 日
Thank you very much!!
congcong
congcong 2015 年 8 月 19 日
But how can I return the counter-part easily? without writing an if statement.(Because I have a large array of elements[3; 4; 7 ; 9;...], not a single element like 3, I want to find the couterpart in a most convenient way.) Sorry, I am really a beginner in this. I appreciate your help!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by