CombinatoricsCombinationsT Method (T, Int32, Boolean) |
Namespace: Accord.Math
public static IEnumerable<T[]> Combinations<T>( this T[] values, int k, bool inPlace = false )
// Let's say we would like to compute all the // combinations of size 2 the elements (1,2,3): // int[] elements = new[] { 1, 2, 3 }; // The number of possible subsets might be too large // to fit on memory. For this reason, we can compute // values on-the-fly using foreach: foreach (int[] combination in Combinatorics.Combinations(elements, k: 2)) { // ... } // Or we could try to compute them all and store in an array: int[][] combinations = Combinatorics.Combinations(elements, k: 2).ToArray(); // In either case, the result will be: int[][] expected = { new [] { 1, 2 }, new [] { 1, 3 }, new [] { 2, 3 }, };