Unique perms

From QETLAB
Jump to navigation Jump to search
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