File Exchange

image thumbnail


version 1.0 (4.21 KB) by

Directly compute Nth lexicographic permutation



View License

Given a SORTED input vector V and positive integer n, rearranges V into its Nth lexicographic permutation. V must be SORTED or the behavior will not be correct. Sorted has flexible meaning however; see internal documentation.

Compare this with the output of my NEXTPERMS submission, which generates a series of sequential lexicographic pemutations in blocks. This instead generates the Nth permutation directly by a factorial radix rebasing; see for more information.

This will be much more efficient than running through the permutations sequentially if the desired permutation is deep in a large set.

Mex C++ and pure Matlab both provided; they should hopefully behave identically.

Basically the same idea as ONEPERM submission, but that submission generates the permutations in a recursive order, similar to the behavior of Matlab's PERMS. NTHPERM generates the permutation in lexicographic order to match NEXTPERMS, which is based on the C++ STL next_permutation function.

Comments and Ratings (0)

MATLAB Release
MATLAB 7.14 (R2012a)

Inspired by: ONEPERM

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video