How to generate permutation of matrix rows in a new matrix?

2 ビュー (過去 30 日間)
Cindy Galdamez
Cindy Galdamez 2020 年 5 月 2 日
コメント済み: KALYAN ACHARJYA 2020 年 5 月 2 日
I have a matrix that will represent a pair of coordinates for each row, so it will be (mx2). For example:
X=[1 2; 3 4; 5 6];
I want to generate a matrix that contains the 6 (3x2x1) possible combinations of these 3 pair of points without repetition. Each 3 rows it would start a new combination until the 6 possible combinations are generated.
Output example:
X1=[3 4; 1 2; 5 6;
1 2; 5 6; 3 4;
5 6; 3 4; 1 2;
1 2; 3 4; 5 6;
3 4; 5 6; 1 2;
5 6; 1 2; 3 4];
Thanks a lot.
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 5 月 2 日
編集済み: KALYAN ACHARJYA 2020 年 5 月 2 日
More coditional statements required-
X=[1 2; 3 4; 5 6];
data=[repelem(X(:,1),6),repelem(X(:,2),6)];
result=[data(randperm(18),1),data(randperm(18),2)]

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

回答 (1 件)

Rik
Rik 2020 年 5 月 2 日
This doesn't result in the same order you write, but it gets the job done:
X=[1 2; 3 4; 5 6];
ind=perms(1:size(X,1));
ind=ind';ind=ind(:);
X1=X(ind,:);
X2=[3 4; 1 2; 5 6;
1 2; 5 6; 3 4;
5 6; 3 4; 1 2;
1 2; 3 4; 5 6;
3 4; 5 6; 1 2;
5 6; 1 2; 3 4];
%confirm they are the same:
X1=sortrows(X1);
X2=sortrows(X2);
clc,isequal(X1,X2)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by