Difference between revisions of "Unique perms"
Jump to navigation
Jump to search
(Created page with "{{Function |name=unique_perms |desc=Computes the unique permutations of a vector |upd=November 27, 2014 |cat=Helper functions |lic=1 |he...") |
m (→Examples: typo) |
||
| Line 15: | Line 15: | ||
==Examples== | ==Examples== | ||
| − | The following generates all unique permutations of the vector <tt>[1,1,2,2,1,2,1,3,3,3]</tt> in two different ways: using this <tt>unique_perms</tt> function, and by using matlab's built-in [http://www.mathworks.com/help/matlab/ref/unique.html unique] and [http://www.mathworks.com/help/matlab/ref/perms.html perms] functions. Using the <tt>unique_perms</tt> function is much faster | + | The following generates all unique permutations of the vector <tt>[1,1,2,2,1,2,1,3,3,3]</tt> in two different ways: using this <tt>unique_perms</tt> function, and by using matlab's built-in [http://www.mathworks.com/help/matlab/ref/unique.html unique] and [http://www.mathworks.com/help/matlab/ref/perms.html perms] functions. Using the <tt>unique_perms</tt> function is much faster when the vector has lots of repetitions (as in this case). |
<syntaxhighlight> | <syntaxhighlight> | ||
>> v = [1,1,2,2,1,2,1,3,3,3]; | >> v = [1,1,2,2,1,2,1,3,3,3]; | ||
Revision as of 16:30, 20 January 2016
| unique_perms | |
| Computes the unique permutations of a vector | |
| Other toolboxes required | none |
|---|---|
| Function category | Helper functions |
| License | license_unique_perms.txt |
| 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.
Syntax
- PERM_LIST = unique_perms(V)
Argument descriptions
- V: The vector to be permuted.
Examples
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).
>> v = [1,1,2,2,1,2,1,3,3,3];
>> tic; length(unique_perms(v)) % fast method
toc
ans =
4200
Elapsed time is 0.300853 seconds.
>> tic; length(unique(perms(v),'rows')) % slow method
toc
ans =
4200
Elapsed time is 4.559448 seconds.Source code
Click here to view this function's source code on github.
External links
- Unique Permutations efficient algorithm: MATLAB Newsgroup post where John D'Errico created this function