How to get all possible combination With the total number of possible combination for "n" variables that can take different values
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Aubai
am 25 Mär. 2024
Kommentiert: Aubai
am 26 Mär. 2024
Hi Guys,
I have "n" variables that my take different values each. (e.g.: length of variable 1 = m1, 2 = m2, 3 = m3 , ...., n = mn)
how can i get the following:
1- what is the total number of possible combination for such problem? is that m1*m2*m3*.....*mn?
2- is there an easy way to get the totale possible combination as a table for those n variables? something like this:
Comb-1 Comb-2 .....................
Var(1) Val(Var(1)[1]) Val(Var(1)[2])
Var(2) Val(Var(2)[1]) Val(Var(2)[1])
Var(3) Val(Var(3)[1]) Val(Var(3)[1])
. . .
. . .
. . .
Var(n) Val(Var(n)[1]) Val(Var(n)[1])
Thanks in Advance
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Voss
am 25 Mär. 2024
a = [1 99];
b = [2 30 700];
c = [4 55 666 8888];
vars = {a,b,c};
n = numel(vars);
out = cell(1,n);
[out{:}] = ndgrid(vars{:});
result = reshape(cat(n+1,out{:}),[],n);
disp(result)
Transpose result if desired.
An option for R2023a or later is to use the combinations function:
result = combinations(a,b,c); % R2023a or later
disp(result)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Exponents and Logarithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!