重复结合 [英] Combinations with repetition
问题描述
我使用的是Mathematica 7并带有combinatorica封装函数,我可以从顺序无关紧要且没有重复的元素列表中获取一定数量的所有组合。例如:
I'm using Mathematica 7 and with a combinatorica package function I can get all combinations of a certain number from a list of elements where the order doesn't matter and there is no repetition.e.g:
in: KSubsets[{a, b, c, d}, 3]
out: {{a, b, c}, {a, b, d}, {a, c, d}, {b, c, d}}
我找不到一个函数,该函数可以从顺序无关紧要且是重复的元素列表中为我提供一定数量的所有组合。
,即上面的示例在输出中将包含诸如{a,a,b},{a,a,a},{b,b,b} ...等元素。
I cannot find a function that will give me all combinations of a certain number from a list of elements where the order doesn't matter and there is repetition. i.e. the above example would include elements like {a,a,b},{a,a,a},{b,b,b}...etc in the output.
它可能需要自定义功能。如果我能提出一个答案,我会发布一个答案,但是现在我看不到一个明显的解决方案。
It may require a custom function. If I can come up with one I will post an answer but for now I don't see an obvious solution.
编辑:
理想情况下,输出不会包含重复的组合,例如
元组[{a,b,c,d},3]
将返回一个包含两个元素的列表,如{a,a,b}和{b,a,a}
从组合的角度来看,它们是相同的。
Ideally the output will not contain duplication of a combination e.g. Tuples[{a, b, c, d}, 3] will return a list that contains two elements like {a,a,b} and {b,a,a} which from a combinations point of view are the same.
推荐答案
DeleteDuplicates[Map[Sort, Tuples[{a, b, c, d}, 3]]]
这篇关于重复结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!