Hello! How can i make a condition run many times?
2 ビュー (過去 30 日間)
古いコメントを表示
Hello! I have a problem. I have an if with a condition that must be fulfilled. But in case it is not, in the else section, i have it run the same code it had before the if. Iunderstand that i may add another else or an if, but it does not seem ok. I actually want something that can make it run until it fulfills the condition. Can you help me?
2 件のコメント
Luna
2019 年 12 月 30 日
Can you be more specific about your condition and what kind of coding you have done so far?
回答 (1 件)
Luna
2019 年 12 月 30 日
編集済み: Luna
2019 年 12 月 30 日
Basically, it can be done as follows with a while block:
some_condition_to_be_fullfilled = 0;
while some_condition_to_be_fullfilled == 0
if something_happened
% do some mathematical stuff
some_condition_to_be_fullfilled = 1;
% or you can use break
break
else
% do some mathematical stuff
some_condition_to_be_fullfilled = 0;
end
end
3 件のコメント
Luna
2020 年 3 月 2 日
編集済み: Luna
2020 年 3 月 2 日
You have problem here:
if [in,on] =0
You can't define an if equality condition like above. You should be doing
if something == 0
Also, you are comparing a 2x1 array with zero, do you want both will be equal to zero? Then you should do this:
if (in == 0) && (on == 0)
Guillaume
2020 年 3 月 2 日
Actually,
if [in, on] == 0
is equivalent to (assuming both in and on are scalar):
if in == 0 && on == 0
but it's likely the OP wouldn't be able to explain why, so Luna's syntax is strongly recommended. Another option would be:
if all([in, on] == 0)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!