How to generate all 2^n combinations of n choices from a 2-vector?
Ältere Kommentare anzeigen
If I want all combinations of 3 choices of 0 or 1, I do this:
t = combinations([0 1],[0 1],[0 1]);
If I want all combinations of 4 choices, I have to do this:
t = combinations([0 1],[0 1],[0 1],[0,1])
If I want all combinations of 5 choices, I have to do this:
t = combinations([0 1],[0 1],[0 1],[0,1],[0 1])
...and so on. Is there any way to generate all combination of a variable number of choices?
Akzeptierte Antwort
Weitere Antworten (1)
n=3;
clear t
[t{n:-1:1}]=ndgrid([0,1]);
t=reshape( cat(n+1, t{:}) , [],n)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!