I'm sure this has been asked a million times, but when I searched all the examples didn't quite fit, so I thought I should ask it anyway.
I have two arrays which will always contain 6 items each. For example:
string[] Colors=
new string[] { "red", "orange", "yellow", "green", "blue", "purple" };
string[] Foods=
new string[] { "fruit", "grain", "dairy", "meat", "sweet", "vegetable" };
Between these two arrays, there are 36 possible combinations(e.g. "red fruit", "red grain").
Now I need to further group these into sets of six unique values.
For example:
meal[0]=
new Pair[] {
new Pair { One="red", Two="fruit" },
new Pair { One="orange", Two="grain" },
new Pair { One="yellow", Two="dairy" },
new Pair { One="green", Two="meat" },
new Pair { One="blue", Two="sweet" },
new Pair { One="purple", Two="vegetable" }
};
where meal is
Pair[][] meal;
No element can be repeated in my list of "meals". So there is only ever a single "Red" item, and a single "meat" item, etc.
I can easily create the pairs based on the first two arrays, but I am drawing a blank on how best to then group them into unique combinations.
C = combntns( v , k ) returns all possible combinations of the set of values v , given combinations of length k .
b = nchoosek( n , k ) returns the binomial coefficient of n and k , defined as n!/(k!( n - k)!) . This is the number of combinations of n items taken k at a time. C = nchoosek( v , k ) returns a matrix containing all possible combinations of the elements of vector v taken k at a time.
Generate All Combinations of Vectors Using the combvec Function. This example shows how to generate a matrix that contains all combinations of two matrices, a1 and a2 . Create the two input matrices, a1 and a2 . Then call the combvec function to generate all possible combinations.
CARTPROD Cartesian product of multiple sets. (The cartesian product of multiple input sets is a larger set containing every ordered combination of the input set elements. See example below.) X = CARTPROD(A,B,C,...) returns the cartesian product of the sets A,B,C, etc, where A,B,C, are numerical vectors.
OK, you want a sequence containing all 720 possible sequences. This is a bit trickier but it can be done.
The basic idea is the same as in my previous answer. In that answer we:
Now we'll do the same thing except instead of producing a permutation at random, we'll produce all the permutations.
Start by getting this library:
http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
OK, we need to make all the permutations of six items:
Permutations<string> permutations = new Permutations<string>(foods);
What do we want to do with each permutation? We already know that. We want to first zip it with the colors array, turning it into a sequence of pairs, which we then turn into an array. Instead, let's turn it into a List<Pair>
because, well, trust me, it will be easier.
IEnumerable<List<Pair>> query =
from permutation in permutations
select colors.Zip(permutation, (color, food)=>new Pair(color, food)).ToList();
And now we can turn that query into a list of results;
List<List<Pair>> results = query.ToList();
And we're done. We have a list with 720 items in it. Each item is a list with 6 pairs in it.
The heavy lifting is done by the library code, obviously; the query laid on top of it is straightforward.
('ve been meaning to write a blog article for some time on ways to generate permutations in LINQ; I might use this as an example!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With