Program to generate permutations in a certain order

2 vues (au cours des 30 derniers jours)
Ronald Niwamanya
Ronald Niwamanya le 24 Mai 2021
Commenté : David Hill le 24 Juil 2021
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 commentaire
Torsten
Torsten le 24 Mai 2021
Modifié(e) : Torsten le 24 Mai 2021
The order of your permutations is not clear to me.
Maybe you want to use Matlab's "perms" to generate all n! permutations.

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 24 Mai 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 commentaires
Ronald Niwamanya
Ronald Niwamanya le 24 Juil 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 le 24 Juil 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(:)';

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Luong
Bruno Luong le 24 Mai 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

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by