How to generate permutation of matrix rows in a new matrix?
1 view (last 30 days)
Show older comments
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 Comment
KALYAN ACHARJYA
on 2 May 2020
Edited: KALYAN ACHARJYA
on 2 May 2020
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)]
Answers (1)
Rik
on 2 May 2020
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)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!