Program to generate permutations in a certain order

2 views (last 30 days)
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 Comment
Torsten
Torsten on 24 May 2021
Edited: Torsten on 24 May 2021
The order of your permutations is not clear to me.
Maybe you want to use Matlab's "perms" to generate all n! permutations.

Sign in to comment.

Accepted Answer

David Hill
David Hill on 24 May 2021
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 Comments
Ronald Niwamanya
Ronald Niwamanya on 24 Jul 2021
@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 on 24 Jul 2021
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(:)';

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 24 May 2021
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

Categories

Find more on Interpolation 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!