1function pu = uniqueperms(vec)
2% list of all unique permutations of a vector with (possibly) replicate elements
3% usage: pu = uniqueperms(vec)
6% vec - 1xn or nx1 vector of elements, replicates allowed
9% pu - mxn array of permutations of vec. Each row
is a permutation.
11% The result should be the same as unique(perms(vec),
'rows')
12% (although the order may be different.)
15% pu = uniqueperms([1 1 1 2 2])
28% See also: unique, perms
30% Author: John D
'Errico
31% e-mail: woodchips@rochester.rr.com
33% Release date: 2/25/08
35% How many elements in vec?
36vec = vec(:); % make it always a column vector
39% how many unique elements in vec?
47 % there was only one unique element, possibly replicated.
50 % all the elements are unique. Just call perms
53 % 2 or more elements, at least one rep
57 ind = find(v==uvec(i),1,
'first');
59 temp = uniqueperms(v);
60 pu{i} = [repmat(uvec(i),size(temp,1),1),temp];