フィルターのクリア

use of "break"

2 ビュー (過去 30 日間)
Mingze Yin
Mingze Yin 2022 年 2 月 28 日
コメント済み: Voss 2022 年 3 月 1 日
Hi everyone,
I have a few lines of code that look smth like this
exist = false;
for j = 1:1:J
if phi(m,j-1,n)*phi(m,j,n)<=0
x0 = SL+(j-2)*dS;
x1 = SL+(j-1)*dS;
y0 = phi(m,j-1,n);
y1 = phi(m,j,n);
boundary = LinearInterpolate(x0,x1,y0,y1);
exist = true;
end
end
First of all, im not confident that this is the right implementation. The purpose of these lines of code is to, by going through a for-loop, check the existence of this "boundary", and once such a boundary exists, set the variable "exist = true". I need this boolean variable because i need to use it for an if-statement immediately after (if exist == true ... else...).
My problem is that I do not think the codes above are executing the purpose as stated above? I kinda sense something is off but I can't quite tell which part isn't right... Would be really thankful if someone could point to me some ways to correct these codes. Thank you!

採用された回答

Voss
Voss 2022 年 2 月 28 日
If you are using the value of boundary immediately after that for loop, then you are using the value of boundary corresponding to the last "boundary" found within the loop.
It's not clear that this is the problem, but, if, for instance, you want the first boundary, you can break immediately after setting exist = true;, which will exit the loop and go to your if exist ... else ... statements.
If that's not the problem, then please describe what the code does vs what you want it to do.
  7 件のコメント
Mingze Yin
Mingze Yin 2022 年 3 月 1 日
Oh thank you so much for this, I actually have been oblivous to this function until now. I tried it and it's already helping me discover some overlooked problems in the program. Thanks again!
Voss
Voss 2022 年 3 月 1 日
You're welcome! That's definitely a useful feature to know about (I use it all the time)

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

その他の回答 (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