gpt4 book ai didi

matlab - Matlab 的 perms 函数中的明显错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:28:12 25 4
gpt4 key购买 nike

 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应该以逆字典顺序显示向量的排列。因此,我希望此输出的最后一行包含元素 0 1 2;但是,它包含 0 2 1。其他行显示正确。

简而言之,最后两行的顺序互换。这是怎么回事?

最佳答案

是的,这似乎是一个错误。接得好!但可能是 documentation 中的错误, 而不是在函数中。

如果您键入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
...

综上所述,该功能似乎是在不考虑任何顺序的情况下设计的。声明该顺序的文档可能是错误的。

关于matlab - Matlab 的 perms 函数中的明显错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31121155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com