Combinations of different rows of a matrix

1 回表示 (過去 30 日間)
Jess
Jess 2019 年 6 月 19 日
回答済み: Stephen23 2019 年 6 月 20 日
I have a 90x2 matrix. I have to generate all combinations of 2 rows of the matrix. How can I do it?
Plz help

採用された回答

Stephen23
Stephen23 2019 年 6 月 20 日
Simpler using basic MATLAB indexing:
>> M = [1,5;6,2;5,6;7,5;9,0]
M =
1 5
6 2
5 6
7 5
9 0
>> X = nchoosek(1:size(M,1),2);
>> R = [M(X(:,1),:),M(X(:,2),:)]
R =
1 5 6 2
1 5 5 6
1 5 7 5
1 5 9 0
6 2 5 6
6 2 7 5
6 2 9 0
5 6 7 5
5 6 9 0
7 5 9 0

その他の回答 (1 件)

Alex Mcaulley
Alex Mcaulley 2019 年 6 月 19 日
A = rand(90,2); %Example
b = nchoosek(1:size(A,1),2);
res = reshape(cell2mat(arrayfun(@(i) A(b(i,:),:),1:size(b,1),'uni',0)),2,size(A,2),[]);
  4 件のコメント
Jess
Jess 2019 年 6 月 19 日
Sir
For example if I have a 5x2 matrix
1 5
6 2
5 6
7 5
9 0
Then my resultant matrix should be a 10 x 4 matrix with the following elements
1 5 1 5 (This should not come as it is repitition)
1 5 6 2
1 5 5 6
1 5 7 5
1 5 9 0
6 2 1 5 (This should not come as it is repitition)
6 2 6 2 (This should not come as it is repitition)
6 2 5 6
6 2 7 5
6 2 9 0
5 6 1 5 (This should not come as it is repitition)
5 6 6 2 (This should not come as it is repitition)
5 6 5 6 (This should not come as it is repitition)
5 6 7 5
5 6 9 0
7 5 1 5(This should not come as it is repitition)
7 5 6 2(This should not come as it is repitition)
7 5 5 6(This should not come as it is repitition)
7 5 7 5(This should not come as it is repitition)
7 5 9 0
9 0 1 5(This should not come as it is repitition)
9 0 6 2(This should not come as it is repitition)
9 0 5 6(This should not come as it is repitition)
9 0 7 5(This should not come as it is repitition)
9 0 9 0(This should not come as it is repitition)
Kindly help me with this
Jess
Jess 2019 年 6 月 20 日
Sir for 90x2 matrix, how should we do it?
Should we type all the elements first
Plz help

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by