我想一次计算向量N
的所有置换k
。我还想允许N
的任何元素与其自身而不仅仅与其他元素交换。我只想得到求和到给定数n
的排列。你知道吗
以下是MATLAB中的一个示例:
N = [1 2 3 4];
k = 2;
n = 6;
对于以上数字,我可以通过以下方式达到目标:
Perm = combvec(N, N)';
Perm = Perm(sum(Perm,2)==n,:);
Perm =
4 2
3 3
2 4
但是,N
的长度预计可达90
个元素,k
的值预计可达10
。这使得上述方法不可行,因为它涉及许多不需要的排列的计算。你知道吗
有没有任何方法可以有效地做到这一点,因为预期的N
向量长度和k
?我很乐意考虑使用MATLAB、R或python的解决方案。你知道吗
这是一个基于生成器的尝试,它只是一次生成一个置换,而不是将它们全部存储在内存中。我使用了生成器方法,因为我认为任何其他方法都会耗尽内存,只要
k
大于5,排列的数量就会很大:输出示例:
似乎你的问题是,是否有一种有效的方法来找到{1,2,…,90}的所有10个元素子集,允许重复,求和为100。你知道吗
很容易计算出存在多少这样的子集:
所以你在问,是否有一种有效的方法来计算和输出一组1.73万亿长度的向量。我认为答案是否定的,这是一个天文数字的向量输出,存储或操纵。你知道吗
相关问题 更多 >
编程相关推荐