Mathematica 枚举组合

Mathematica enumerate combinations

我需要枚举我拥有的 3 组值的组合。例如,这些组是 (a,b,c,d), (e,f,g,h), (i,j,k,l)。总组合为 4x4x4=64。 有谁知道,我该如何定义这些组合的升序编号? 我已经用那种形式写了一些东西:

Do[Do[Do[x["formula is needed here"]=s[[i,j,k]],{k,1,4}],{j,1,4}],{i,1,4}]

我找不到组合编号的公式。我已经阅读了一些关于 "Generating the mth Lexicographical Element of a Mathematical Combination" 的内容,但我迷路多于帮助。 x 应该取值 1,2,3,.....,64。 感谢您的建议!

Tuples[{{a, b, c, d}, {e, f, g, h}, {i, j, k, l}}]

如果你需要 "formula" 作为 'nth' 元组,它看起来像这样:

{ Floor[(# - 1)/16      ] + 1,
  Floor[Mod[# - 1, 16]/4] + 1 , 
  Floor[Mod[# - 1, 4]   ] + 1 } & /@ Range[64] ==
  Tuples[Range[4], 3]

True

那么如果你想说第 12 个组合,你可以这样做:

({ 
     Floor[(# - 1)/16] + 1,
     Floor[Mod[# - 1, 16]/4 + 1] ,
     Mod[# - 1, 4] + 1 } &@12);
{{a, b, c, d}[[%[[1]]]], {e, f, g, h}[[%[[2]]]], {i, j, k, 
   l}[[%[[3]]]]}

{a, g, l}

请注意,无论您在做什么,几乎总是最好使用内置的面向对象函数。

 Tuples[{{a, b, c, d}, {e, f, g, h}, {i, j, k, l}}][[12]]

{a, g, l}

编辑:为了完整起见,对第一个表达式进行概括:

listlen = 6;
nsamp = 4;
Table[Floor[Mod[# - 1, listlen^i]/listlen^(i - 1) + 1], {i, nsamp, 
     1, -1}] & /@ Range[listlen^nsamp] ==
  Tuples[Range[listlen], nsamp] 

True