Program to generate permutations in a certain order

2 次查看(过去 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 个评论
Torsten
Torsten 2021-5-24
编辑:Torsten 2021-5-24
The order of your permutations is not clear to me.
Maybe you want to use Matlab's "perms" to generate all n! permutations.

请先登录,再进行评论。

采纳的回答

David Hill
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 个评论
Ronald Niwamanya
Ronald Niwamanya 2021-7-24
@David Hill Just a quick one, Considering the permutations from the above e.g from m4, how can you reverse any of the permutations to obtain the original sequence X i.e 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]?
Thank you very much.
David Hill
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
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{:})
P = 4×24
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 1 0 1 1 1 1 1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 1 1 1 1 0

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by