Selecting unique pairs from a vector

25 ビュー (過去 30 日間)
jlt199
jlt199 2016 年 9 月 9 日
コメント済み: jlt199 2016 年 9 月 9 日
Afternoon all,
Suppose I have a vector
imax = [1;2;3;4;5;6];
I would like to find every set of pairs available in that matrix, so that
pairs = [1,2; 1,3; 1,4; 1,5; 1,6; 2,3; 2,4; 2,5; 2,6; 3,4; 3,5; 3,6; 4,5; 4,6; 5,6];
Where if we have the pair 1,2 we don't also have the pair 2,1. I have got this far, but it's obviously not working. Can anyone help me over the last little hurdle?
% Find every pairwise combination of indexes
[p,q] = meshgrid(imax, imax);
pairs = [p(:) q(:)];
pairs(pairs(:,1)==pairs(:,2),:) = [];
Many thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 9 月 9 日
編集済み: Azzi Abdelmalek 2016 年 9 月 9 日
imax = [1;2;3;4;5;6]
out=nchoosek(imax,2)
  1 件のコメント
jlt199
jlt199 2016 年 9 月 9 日
That's a lot simpler than my answer:
[p,q] = meshgrid(imax, imax);
pairs = [p(:) q(:)];
pairs(pairs(:,1)>=pairs(:,2),:) = [];
Thank you very much

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

その他の回答 (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