Just a thought guys, how would you rewrite this loop construct?
if i < 4
subplot(4,1,i+1);
plot(x,y);
title('Original');
end
I did try to write it as follows but something went wrong outside the loop:
subplot(4,1,i+1);
plot(x,y);
title('Original');
if i==2
break
end
What do you think of other loop constructs?

5 件のコメント

Amit
Amit 2014 年 2 月 1 日
編集済み: Amit 2014 年 2 月 1 日
where is the loop? I mean what you're trying to do?
Ayesha
Ayesha 2014 年 2 月 1 日
there's this if loop which I'm wondering if there could be an alternative for it. I'll write the code again which is my main subject of concern:
if i < 4
subplot(4,1,i+1);
plot(x,y);
title('Original');
end
Walter Roberson
Walter Roberson 2014 年 2 月 1 日
That is not a loop. "if" does not cause looping: the body of the "if" is done once if the condition is true. Using a loop requires "for" or "while"
Ayesha
Ayesha 2014 年 2 月 1 日
So could if be replaced by any other construct?
Walter Roberson
Walter Roberson 2014 年 2 月 1 日
What kind of alternative were you hoping for? It's just an "if" statement with a simple body.

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

 採用された回答

Sneheet
Sneheet 2014 年 2 月 1 日
編集済み: Sneheet 2014 年 2 月 1 日

0 投票

if you want to plot for i=1 to i=4,
for i=1:4
subplot(4,1,i+1);
plot(x,y);
title('Original');
end

1 件のコメント

Walter Roberson
Walter Roberson 2014 年 2 月 1 日
編集済み: Walter Roberson 2014 年 2 月 1 日
To be consistent, the upper end of the loop should be 3 rather than 4. The lower end could be adjusted to 0 instead of 1.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 2 月 1 日

編集済み:

2014 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by