|Computes the unique permutations of a vector|
|Other toolboxes required||none|
|Function category||Helper functions|
|This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.|
unique_perms is a function that computes all distinct permutations of a vector. Thus is produces the same output as using the built-in MATLAB commands unique(perms(V),'rows'), but it is significantly faster if there are many repetitions in the vector.
- PERM_LIST = unique_perms(V)
- V: The vector to be permuted.
The following generates all unique permutations of the vector [1,1,2,2,1,2,1,3,3,3] in two different ways: using this unique_perms function, and by using matlab's built-in unique and perms functions. Using the unique_perms function is much faster when the vector has lots of repetitions (as in this case).
Click on "expand" to the right to view the MATLAB source code for this function.
%% UNIQUE_PERMS Computes the unique permutations of a vector
% This function has one required argument:
% V: a vector
% PERM_LIST = unique_perms(V) is list of unique permutations of a vector.
% This function does the same thing as unique(perms(v),'rows'), but is
% much faster and less memory-intensive in most cases.
% URL: http://www.qetlab.com/unique_perms
% requires: nothing
% author: John D'Errico
% package: QETLAB
% last updated: November 27, 2014
function perm_list = unique_perms( v )
uv = unique(v);
n = length(v);
nu = length(uv);
if nu <= 1
perm_list = v;
elseif n == nu
perm_list = perms(v);
perm_list = cell(nu,1);
for j = 1:nu
vt = v;
vt(find(vt==uv(j),1)) = ;
t = unique_perms(vt);
perm_list = cell2mat(perm_list);
- Unique Permutations efficient algorithm: MATLAB Newsgroup post where John D'Errico created this function