How to get all possible combination With the total number of possible combination for "n" variables that can take different values
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aubai
el 25 de Mzo. de 2024
Comentada: Aubai
el 26 de Mzo. de 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 comentarios
Respuesta aceptada
Más respuestas (1)
Voss
el 25 de Mzo. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!