3.0

3.0 | 1 rating Rate this file 1 Download (last 30 days) File Size: 1.3 KB File ID: #31184

NUPerms

by Alessandro Colombo

 

25 Apr 2011

Next Unique Permutation of an integer vector

| Watch this File

File Information
Description

[PermOut, last] = NUPerms(PermIn) uses a simple and efficient iterative algorithm to return the permutation vector that follows PermIn in lexicographical order, discarding replicates.

This is much faster than finding all permutations with perms and then discarding replicates. Moreover, calling the function iteratively, one can explore all permutations of a given vector without having to store them in memory.

If PermIn is the last vector in lexicographical order, then PermOut=PermIn and last is set to 1, otherwise last is set to 0.

[P,last]=NUPerms([0 0 1])
  P = 0 1 0
  last = 0

[P,last]=NUPerms([1 0 0])
  P = 1 0 0
  last = 1

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File  
Everyone's Tags
perms, replicates, unique
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
11 Jul 2012 David Holdaway

Can't say I'm convinced this is quicker than just generating all the permutations with perms and post selecting, calling very quick functions recursively in Matlab is not efficient at all really.
If you just want just ONE permutation it is obviously faster and not having to store them in memory is probably nice.

There is one thing I am however confused about, which is why you have made this work only for integers, there is nothing in the logic that would mean it shouldn't work fine for double precision numbers why not let the function use the same class as the input?

Contact us