How to obtain discernable permutations of a vector ?
Show older comments
Hi everyone,
I have the following problem : let's say I have the vector v=(1,1,1,0,0) and I want to generate the permutations of this vector. The problem is, if I simply use perms(v), matlab is going to generate many degenerate vectors because it does not understand that the elements of my vector are similar (so for instance I will have like ten exemplars of (1,0,1,1,0) , then ten of (0,1,1,1,0), etc). Of course I can easily make a code that gets rid of those degenerate vectors, but it is very time consuming when the length of the vector gets bigger.
Thank you for your help guys.
Alex.
Accepted Answer
More Answers (1)
szenicer
on 18 Nov 2014
0 votes
2 Comments
Roger Stafford
on 18 Nov 2014
x = [1,1,1,0,0];
n = size(x,2);
k = nnz(x);
C = nchoosek(1:n,k);
m = size(C,1);
A = zeros(m,n);
for ix = 1:m
A(ix,C(ix,:)) = 1;
end
The ten rows of A will have all possible distinct permutations of the 1's and 0's in x.
szenicer
on 20 Nov 2014
Categories
Find more on Profile and Improve Performance 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!