My strange nested for loops

1 回表示 (過去 30 日間)
Martin
Martin 2015 年 8 月 5 日
編集済み: Martin 2015 年 8 月 5 日
Hello buddies,
I've gotten myself into a bit problematic nested if loop. And I have hard time thinking this out.
Imagine 3 for loops and 1 if.
if true
for Ll=LL:LH
for Lh=LL:LH
if Ll<=Lh
for i=Ll:Lh
end
My settings is LL and LH e.g: LL=1 and LH =3 (Both can be higher and with higher range). Then I would like i to make all combinations as long Ll<=Lh - and just one time!
in this example I get the following for i's:
if true
Ll=1 and Lh=1 -> for i=1:1
Ll=1 and Lh=2 -> for i=1:2 * repeats the 1:1 as above
Ll=1 and Lh=3 -> for i=1:3 * repeats the 1:1 as above
Ll=2 and Lh=2 -> for i=2:2
Ll=2 and Lh=3 -> for i=2:3 * repeats the 2:2 as above
Ll=3 and Lh=3 -> for i=3:3
end
The problem is that the above for-i-loops repeat them self a couple of times (the one marked with stars).
Hope to hear from someone! Have a good day, -best

採用された回答

Subin Kuttappan Stellal Mary
Subin Kuttappan Stellal Mary 2015 年 8 月 5 日
編集済み: Subin Kuttappan Stellal Mary 2015 年 8 月 5 日
In order for i to make all combinations as long Ll<=Lh, the third for loop is not required.
Try this:
for Ll=LL:LH
for Lh=LL:LH
if Ll<=Lh
fprintf('%d %d\n',Ll,Lh);
end
end
end
  1 件のコメント
Martin
Martin 2015 年 8 月 5 日
編集済み: Martin 2015 年 8 月 5 日
ok thx, it is fixed now

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by