How to generate permutation of matrix rows in a new matrix?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
KALYAN ACHARJYA
el 2 de Mayo de 2020
Editada: KALYAN ACHARJYA
el 2 de Mayo de 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)]
Respuestas (1)
Rik
el 2 de Mayo de 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)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!