Random number of nested for loops

Dear all,
I have the following problem: I want to input an integer number 'N', then inside the program I need to generate automatically 'N' nested for loops. For example:
*If I put N=2, I need the program by itself to make the following:
for i=0:1
for j=0:1
...
end
end
*If I put N=3, I need the program by itself to make the following:
for i=0:1
for j=0:1
for j=0:1
...
end
end
end
and so on.
Thank you in advance.
Hadi.

4 comentarios

Mischa Kim
Mischa Kim el 27 de Feb. de 2015
Hadi, maybe there's better ways to go about solving your problem. What exactly do you need your loops to do?
hadi
hadi el 27 de Feb. de 2015
Thank you Mischa for your reply, actually I have random integer number (N) of variables that takes the values 0 or 1, so I want to generate all the combinations of these variables.
Javier Vicente
Javier Vicente el 27 de Feb. de 2015
Hi Hadi.
Check nchoosek function. Maybe works for you.
hadi
hadi el 27 de Feb. de 2015
Sorry Mischa, but it seems not working.
in more details what I need, if N=2, I want the following output:
00
01
10
11
if N=3:
000
001
010
011
100
101
110
111
and so on....and that is not working with nchoosek function.

Iniciar sesión para comentar.

 Respuesta aceptada

Mischa Kim
Mischa Kim el 27 de Feb. de 2015
Hadi, without any further requirements on output data type, etc., how about
myarray = dec2bin([0:2^N-1],N-1)

1 comentario

hadi
hadi el 27 de Feb. de 2015
Thank you so much Mischa, wish you a nice weekend.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 27 de Feb. de 2015

Comentada:

el 27 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by