필터 지우기
필터 지우기

Random number of nested for loops

조회 수: 3 (최근 30일)
hadi
hadi 2015년 2월 27일
댓글: hadi 2015년 2월 27일
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
Javier Vicente
Javier Vicente 2015년 2월 27일
Hi Hadi.
Check nchoosek function. Maybe works for you.
hadi
hadi 2015년 2월 27일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Mischa Kim
Mischa Kim 2015년 2월 27일
Hadi, without any further requirements on output data type, etc., how about
myarray = dec2bin([0:2^N-1],N-1)
  댓글 수: 1
hadi
hadi 2015년 2월 27일
Thank you so much Mischa, wish you a nice weekend.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by