NUPerms

Next Unique Permutation of an integer vector
168 Downloads
Updated 25 Apr 2011

View License

[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

Cite As

Alessandro Colombo (2024). NUPerms (https://www.mathworks.com/matlabcentral/fileexchange/31184-nuperms), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Directed Graphs in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0