p = perms([0:2])P=
2 1 0
2 0 1
1 2 0
1 0 2
0 1 2
0 2 1这一功能应该以反向字典顺序显示向量的排列。因此,我希望这个输出的最后一行包含元素0 1 2;但是,它包含0 2 1。其他行将正确显示。
简而言之,最后两行的顺序是互换的。这里发生什么事情?
发布于 2015-06-29 17:16:55
是的,这好像是个虫子。抓得好!但可能是文档中的错误,而不是函数中的错误。
如果您输入open perms来查看源代码,您将在第一行中看到以下描述:
%PERMS All possible permutations.
% PERMS(1:N), or PERMS(V) where V is a vector of length N, creates a
% matrix with N! rows and N columns containing all possible
% permutations of the N elements.
%
% This function is only practical for situations where N is less
% than about 10 (for N=11, the output takes over 3 gigabytes).
%
% Class support for input V:
% float: double, single
% integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
% logical, char其中没有提到颠倒词典的顺序。
实际工作由递归的局部函数permsr完成。如果查看它的代码,一开始并不清楚它是如何工作的(与通常的递归一样),但是行
t(t == i) = n给出了一个线索,即在结果中没有寻找特定的顺序。
如果尝试更大的向量,则会发现更多行的反向字典顺序存在差异:
>> 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 and 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 and 2
1 2 3 0
1 2 0 3
1 3 2 0 %// here. Affects cols 2 and 3
...总之,该函数的设计似乎没有考虑到任何顺序。这是文件,可能是错误的要求,该订单。
https://stackoverflow.com/questions/31121155
复制相似问题