Generating Combinations
Every time I have to do this I "invent" a different way. Time to standardize. I suspect there is some default command I overlooked开发者_C百科 ready to do this, so I am sorry in advance if the question is too trivial.
What is the better (memory, performance) way to get:
combinations[{1,2,3},2] = {{1,2},{1,3},{2,3}}
with arbitrary elements in the input list, of course.
Subsets[{1, 2, 3}, {2}]
is the built-in way.
Before Subsets
was added as a core function, the Combinatorica function KSubsets
was available.
Needs["Combinatorica`"]
KSubsets[{1, 2, 3}, 2]
(* {{1, 2}, {1, 3}, {2, 3}} *)
Combinatorica still provides additional functionality, such as NextKSubset
:
NextKSubset[{1, 2, 3}, {1, 3}]
(* {2, 3} *)
This last function can be very helpful for memory management.
精彩评论