gpt4 book ai didi

algorithm - Matlab 中的排列函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:37:35 25 4
gpt4 key购买 nike

在 Wolfram Mathematica 中,有一个名为 Permutations( http://reference.wolfram.com/mathematica/ref/Permutations.html ) 的函数。它可以给出恰好包含 n 个元素的所有排列。

例如:Permutations[{1,2,3,4}, {2}] 给出

{{1, 2}, {1, 3}, {1, 4}, {2, 1}, {2, 3}, {2, 4}, {3, 1}, { 3, 2}, {3,
4}, {4, 1}, {4, 2}, {4, 3}}

我知道 Matlab 有一个类似的函数 perms,但它只接收一个参数并给出所有可能的排列。有没有像Mathematica的Permutations[list,{n}]这样的函数?

最佳答案

如果顺序无关紧要,请查看nchoosek

如果是这样(似乎是这种情况),则存在一种低效、丑陋但不依赖于工具箱的单行代码:

>> unique(builtin('_paren', perms(1:4), :,1:2), 'rows')
ans =
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3

(实际上是 a hacked two-liner )。

我建议您只使用统计工具箱中的 combnk

关于algorithm - Matlab 中的排列函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516709/

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