math - Apparent error in Matlab's perms function -


 p = perms([0:2]) 

p =

 2     1     0  2     0     1  1     2     0  1     0     2  0     1     2  0     2     1 

this function supposed display permutations of vector in reverse lexicographical order. hence, expect last row of output contain elements 0 1 2; however, contains 0 2 1. other rows displayed correctly.

in short, order of last 2 rows interchanged. going on here?

yes, seems bug. catch! bug in documentation, rather in function.

if type open perms see source code, you'll see following description in first lines:

%perms  possible permutations. %   perms(1:n), or perms(v) v vector of length n, creates %   matrix n! rows , n columns containing possible %   permutations of n elements. % %   function practical situations n less %   10 (for n=11, output takes on 3 gigabytes). % %   class support input v: %      float: double, single %      integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 %      logical, char 

in no reference made reverse lexicographical order.

the actual job done recursive, local function permsr. if @ code, it's not obvious @ first how works (as usual recursion), line

t(t == i) = n 

gives clue no particular order sought in result.

if try larger vector you'll see discrepancies reverse lexicographical order in more rows:

>> perms(0:3) ans =      3     2     1     0      3     2     0     1      3     1     2     0      3     1     0     2      3     0     1     2      3     0     2     1   %// here. affects cols 1 , 2      2     3     1     0      2     3     0     1      2     1     3     0      2     1     0     3      2     0     1     3      2     0     3     1   %// here. affects cols 1 , 2      1     2     3     0      1     2     0     3      1     3     2     0   %// here. affects cols 2 , 3      ... 

in summary, function seems have been designed without regard order. documentation wrong in claiming order.


Comments

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

javascript - Create websocket without connecting -

sharepoint - Accessing files across a shared directory using a Windows service -