How to get back to the beginning of a loop when the loop ends
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Star Strider
2014-4-28
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!