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

1 Ansicht (letzte 30 Tage)
Cindy Galdamez
Cindy Galdamez am 2 Mai 2020
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 Kommentar
KALYAN ACHARJYA
KALYAN ACHARJYA am 2 Mai 2020
Bearbeitet: KALYAN ACHARJYA am 2 Mai 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)]

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 2 Mai 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)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by