Program to generate permutations in a certain order
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ronald Niwamanya
il 24 Mag 2021
Commentato: David Hill
il 24 Lug 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 Commento
Risposta accettata
David Hill
il 24 Mag 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 Commenti
David Hill
il 24 Lug 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(:)';
Più risposte (1)
Bruno Luong
il 24 Mag 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{:})
0 Commenti
Vedere anche
Categorie
Scopri di più su Language Fundamentals in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!