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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Creating 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