perms1
returns a single permutation of the numbers 1:n
Author: Khaled Hamed
Hamed, Khaled
I have submitted a new function 'recperms' which uses a similar logic, but calculates the permutations recursively, and thus no index is needed. The code is about 2.5 times faster than perms1 (still very slow compared to perms) and avoids the problem with large indices noted by John D'Errico. Thanks John.
21 Nov 2006
perms1
returns a single permutation of the numbers 1:n
Author: Khaled Hamed
D'Errico, John
Excellent. The author has included error checks, a description of the algorithm, many internal comments so one can understand how the code works. Each call is fast too.
Two things to beware (not the fault of the author) large values of n will still be a problem. For example,
perms1(20,2e18)
and
perms1(20,2e18+1)
will generate exactly the same result. This is an artifact of double precision in matlab. So you don't want to go above n=18 in perms1.
Secondly, perms1 does not generate its permutations in the same order as perms. I'd argue that the order chosen by perms1 is more logical than perms anyway.