フィルターのクリア

How to get back to the beginning of a loop when the loop ends

2 ビュー (過去 30 日間)
John
John 2014 年 4 月 28 日
コメント済み: Star Strider 2014 年 4 月 28 日
Okay my question might sound confusing, but here's what I'm trying to do.
I'm supposed to do some calculation within a loop, and then, at the end of that loop, I use whatever I get out to plug back into the beginning of the loop and basically start that same loop again. For example,
n = 1;
for n < 20
a = 20 + r
V = a + 4
if V < 20
some calculation
V = 4 * x + y
...
end
n = n + 1;
end
So, at the end, I get V equal to something.
Then I want to use this new value of V that I just got and put it back into the above loop to evaluate if this new V is less than 20. If it is, then perform everything in that loop again.
How do I do this?
Thank you so much!

回答 (1 件)

Star Strider
Star Strider 2014 年 4 月 28 日
Use a while loop:
r = 0.1; n = 1;
while n < 20
a = 20 + r
V = a + 4
if V < 20
some calculation
V = 4 * x + y
...
end
n = n + 1;
end
  2 件のコメント
John
John 2014 年 4 月 28 日
The 'r' in my question is just some input that the user has to enter.
So, how would you form that while loop exactly? I already have a while loop going on.
Thank you
Star Strider
Star Strider 2014 年 4 月 28 日
Just the way I listed here. You can nest while and other types of loops.
I needed a value for r to test the code.

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

カテゴリ

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