How can I generate all possible combinations from multiple sets of nchoosek?

3 visualizaciones (últimos 30 días)
Basically, I want to run combntns on multiple sets. Given multiple vectors and knowing how many elements to choose from each vector, can I generate all possible combinations? I suppose it can be done with for loops, but I am hoping for something more elegant.
Example
If
n1 = [1,2,3]; k1 = 1;
n2 = [4,5,6]; k2 = 2;
then
c = [1 4 5
1 4 6
1 5 6
2 4 5
2 4 6
2 5 6
3 4 5
3 4 6
3 5 6]

Respuesta aceptada

Matthew Eicholtz
Matthew Eicholtz el 5 de Sept. de 2015
In case anyone else ever stumbles upon this question...I figured out how to solve my problem. Use a combination of nchoosek and combvec:
c1 = nchoosek(n1,k1);
c2 = nchoosek(n2,k2);
c = combvec(c1',c2')';
To order the rows in the same way as shown in the question, use:
csorted = sortrows(c);

Más respuestas (0)

Categorías

Más información sobre Discrete Math en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by