Program to generate permutations in a certain order
조회 수: 3 (최근 30일)
이전 댓글 표시
Greetings.
I need to write a program that can generate m permutations from a possible number of permutation sequences n!. (m<n!).
Below is a description. Thank you very much and be blessed.
X=[X1 X2 X3 X4 X5 X6];
%Where
X1=[1 1 0 1];
X2=[1 0 1 0];
X3=[1 0 1 1];
X4=[1 1 1 0];
X5=[0 1 0 1];
X6=[0 0 0 1];
% generate a matrix that contains k permutations out of a possible number
% % of 6!=720. Forexample k=4.
%generate the first 4 permutations (1-4)
P=X1 X2 X3 X4 X5 X6; X1 X2 X3 X4 X6 X5
X1 X2 X3 X5 X4 X6; X1 X2 X3 X5 X6 X4
%Which will be P=[1 1 0 1 1 0 1 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 0 1;
......
....1 1 0 1 1 0 1 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0]
% generate last 4 permutations (717-720)
P=X6 X5 X4 X2 X3 X1; X6 X5 X4 X3 X1 X2
X6 X5 X4 X3 X2 X1; X1 X2 X3 X4 X5 X6
% generate the 4 permutations e,g 520-523
% generate the 4 permuations randomly (e.g 230, 310, 320, 640)
%generate the 4 permuations one after the other (e.g 1, 3 , 5, 7 or 210,
%212, 214, 216)
댓글 수: 1
채택된 답변
David Hill
2021년 5월 24일
k=4;
X=[1 1 0 1;1 0 1 0;1 0 1 1;1 1 1 0;0 1 0 1;0 0 0 1]';
p=perms(6:-1:1);
m1=reshape(X(:,p(1:k,:)'),24,[])';
m2=reshape(X(:,p(end-k+1:end,:)'),24,[])';
m3=reshape(X(:,p(520:523,:)'),24,[])';
m4=reshape(X(:,p(randi(720,1,4),:)'),24,[])';
m5=reshape(X(:,p(1:2:7,:)'),24,[])';
댓글 수: 3
David Hill
2021년 7월 24일
You have to know the order of one of the permutations of the rows of m. For example is you know that the first row of m4 permuted x by [5 3 6 4 1 2], then it would be easy to generate x.
m=reshape(m4(1,:),4,[]);%generate the row of m4 for the known permutation
idx=[5 3 6 4 1 2];%known permutation of x for the 1st row of m4
x=x(:,idx);
x=x(:)';
추가 답변 (1개)
Bruno Luong
2021년 5월 24일
clear X
X{1}=[1 1 0 1];
X{2}=[1 0 1 0];
X{3}=[1 0 1 1];
X{4}=[1 1 1 0];
X{5}=[0 1 0 1];
X{6}=[0 0 0 1];
n=length(X);
P=flipud(perms(1:n));
c=arrayfun(@(r) cat(2,X{P(r,:)}), 1:4,'Unif',0);
P=cat(1,c{:})
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!