Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Combinations in combinatorics have a special meaning. Accordingly, you can calculate the combinations for more dimensions. Note that these always contain the indices of diagonal elements. See threads on StackOverflow here (combinations)here (combinations) and here (combinations with repetition)here (combinations with repetition).

This solution is the function version of the answer provided by Yaroslav Bulatov (link above):

Combinations[elem_List] := Combinations[elem, All]; Combinations[elem_List, All | Full] := Combinations[elem, Length@Union@elem]; Combinations[elem_List, n_Integer] := Module[{coef2vars, coefs, set}, set = Union@elem; coef2vars[list_] := Join @@ (MapIndexed[Table[set[[First@#2]], {#1}] &, list]); coefs = Flatten[Permutations /@ IntegerPartitions[n, {Length@set}, Range[0, n]], 1]; Sort@(coef2vars /@ coefs) ]; Combinations[{1, 2, 3, 4}, 3] 
{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, {1, 2, 2}, {1, 2, 3}, {1, 2, 4}, {1, 3, 3}, {1, 3, 4}, {1, 4, 4}, {2, 2, 2}, {2, 2, 3}, {2, 2, 4}, {2, 3, 3}, {2, 3, 4}, {2, 4, 4}, {3, 3, 3}, {3, 3, 4}, {3, 4, 4}, {4, 4, 4}} 

Combinations in combinatorics have a special meaning. Accordingly, you can calculate the combinations for more dimensions. Note that these always contain the indices of diagonal elements. See threads on StackOverflow here (combinations) and here (combinations with repetition).

This solution is the function version of the answer provided by Yaroslav Bulatov (link above):

Combinations[elem_List] := Combinations[elem, All]; Combinations[elem_List, All | Full] := Combinations[elem, Length@Union@elem]; Combinations[elem_List, n_Integer] := Module[{coef2vars, coefs, set}, set = Union@elem; coef2vars[list_] := Join @@ (MapIndexed[Table[set[[First@#2]], {#1}] &, list]); coefs = Flatten[Permutations /@ IntegerPartitions[n, {Length@set}, Range[0, n]], 1]; Sort@(coef2vars /@ coefs) ]; Combinations[{1, 2, 3, 4}, 3] 
{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, {1, 2, 2}, {1, 2, 3}, {1, 2, 4}, {1, 3, 3}, {1, 3, 4}, {1, 4, 4}, {2, 2, 2}, {2, 2, 3}, {2, 2, 4}, {2, 3, 3}, {2, 3, 4}, {2, 4, 4}, {3, 3, 3}, {3, 3, 4}, {3, 4, 4}, {4, 4, 4}} 

Combinations in combinatorics have a special meaning. Accordingly, you can calculate the combinations for more dimensions. Note that these always contain the indices of diagonal elements. See threads on StackOverflow here (combinations) and here (combinations with repetition).

This solution is the function version of the answer provided by Yaroslav Bulatov (link above):

Combinations[elem_List] := Combinations[elem, All]; Combinations[elem_List, All | Full] := Combinations[elem, Length@Union@elem]; Combinations[elem_List, n_Integer] := Module[{coef2vars, coefs, set}, set = Union@elem; coef2vars[list_] := Join @@ (MapIndexed[Table[set[[First@#2]], {#1}] &, list]); coefs = Flatten[Permutations /@ IntegerPartitions[n, {Length@set}, Range[0, n]], 1]; Sort@(coef2vars /@ coefs) ]; Combinations[{1, 2, 3, 4}, 3] 
{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, {1, 2, 2}, {1, 2, 3}, {1, 2, 4}, {1, 3, 3}, {1, 3, 4}, {1, 4, 4}, {2, 2, 2}, {2, 2, 3}, {2, 2, 4}, {2, 3, 3}, {2, 3, 4}, {2, 4, 4}, {3, 3, 3}, {3, 3, 4}, {3, 4, 4}, {4, 4, 4}} 
Source Link
István Zachar
  • 47.2k
  • 20
  • 147
  • 307

Combinations in combinatorics have a special meaning. Accordingly, you can calculate the combinations for more dimensions. Note that these always contain the indices of diagonal elements. See threads on StackOverflow here (combinations) and here (combinations with repetition).

This solution is the function version of the answer provided by Yaroslav Bulatov (link above):

Combinations[elem_List] := Combinations[elem, All]; Combinations[elem_List, All | Full] := Combinations[elem, Length@Union@elem]; Combinations[elem_List, n_Integer] := Module[{coef2vars, coefs, set}, set = Union@elem; coef2vars[list_] := Join @@ (MapIndexed[Table[set[[First@#2]], {#1}] &, list]); coefs = Flatten[Permutations /@ IntegerPartitions[n, {Length@set}, Range[0, n]], 1]; Sort@(coef2vars /@ coefs) ]; Combinations[{1, 2, 3, 4}, 3] 
{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, {1, 2, 2}, {1, 2, 3}, {1, 2, 4}, {1, 3, 3}, {1, 3, 4}, {1, 4, 4}, {2, 2, 2}, {2, 2, 3}, {2, 2, 4}, {2, 3, 3}, {2, 3, 4}, {2, 4, 4}, {3, 3, 3}, {3, 3, 4}, {3, 4, 4}, {4, 4, 4}}