Code doesn't terminate in the way that I want

2 ビュー (過去 30 日間)
Matthew Hunt
Matthew Hunt 2022 年 3 月 8 日
コメント済み: Jan 2022 年 3 月 9 日
I have a loop which changes the value of a vector at each iteration of the loop. I don't want the values in the loop to get greater or smaller than predefined values. The code I'm using for this is:
if (min(u_new)<0.02 || max(u_new)>0.98)
return;
end
This doesn't seem to work. I thought that this would be a relatively simple thing to prgram but it's being stubburn.

回答 (1 件)

Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 8 日
編集済み: Max Alger-Meyer 2022 年 3 月 9 日
So I'm not totally sure what you're asking the code to do, but using 'return' is going to end your script or function. If you just want to exit the loop, use 'break' instead. Alternatively, if you just want the loop to skip over the values that would fall outside of your desired range and keep iterating, you'd want to flip the signs in the 'if' condition and change the values in the body of the if statement, once the criteria are met. If you post additional code so I can see what you are trying to do I can help more.
  7 件のコメント
Matthew Hunt
Matthew Hunt 2022 年 3 月 9 日
The norm(c) was just to output the value of norm of c so I could see the change in the value of c as time progressed.
Jan
Jan 2022 年 3 月 9 日
@Matthew Hunt: And it does break the loop, if the condition is met:
min(u_new)<0.02 || max(u_new)>0.98
If the loop is not left, the condition is not true. You wrote "This doesn't seem to work", but of course it works exactly as expected. The problem is, that your u_new has other values than you expect. You can check this by your own or provide the input values.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by