Click or drag to resize
Accord.NET (logo)

CombinatoricsCombinationsT Method (T, Int32, Boolean)

Enumerates all possible value combinations for a given array.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static IEnumerable<T[]> Combinations<T>(
	this T[] values,
	int k,
	bool inPlace = false
)
Request Example View Source

Parameters

values
Type: T
The array whose combinations need to be generated.
k
Type: SystemInt32
The length of the combinations to be generated.
inPlace (Optional)
Type: SystemBoolean
If set to true, the different generated combinations will be stored in the same array, thus preserving memory. However, this may prevent the samples from being stored in other locations without having to clone them. If set to false, a new memory block will be allocated for each new object in the sequence.

Type Parameters

T

Return Value

Type: IEnumerableT

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Examples
// 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 },
};
See Also