MATLAB Answers

For loop with two conditions

3 ビュー (過去 30 日間)
Saad Alqahtani
Saad Alqahtani 2021 年 7 月 5 日
コメント済み: Saad Alqahtani 2021 年 7 月 6 日
Hi,
I'm trying to run this code where it should stop as soon as one of the conditions has met and give me the index where it stops.
So, here is my code and it's taking so long to run and it also didn't stop utill the end which I'm sure one of those conditions should have been met far away from the end.
isOK1= (Discard2value<=target_value_30_P);
isOK2 =(Discard2value>=t2);
for N = datan(1:end)
while 1
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
break
end
end
end
I don't know what's missing? Any help would be appreciate it.
Also, I keep getting this error : "array indices must be positive integers or logical values"
Thanks in advance.

採用された回答

DGM
DGM 2021 年 7 月 5 日
編集済み: DGM 2021 年 7 月 5 日
Considering all the comparisons that are being made between unknown variables, I'm not eager to guess at what should be expected. I doubt that any of these loops are necessary, given that all the test values appear to be scalar logicals defined outside the loops. In fact, since nothing changes within the loops, the inner loop serves no purpose and will either pass trivially, or it will get stuck. I also assume that it's unlikely that (presumably) floating point values in datan are exactly 1 or 0, so it's probably never going to do anything but get stuck.
If that doesn't reveal the problem, include enough supporting example data that the problem can be reproduced, or provide enough of the surrounding code that the intent is unambiguous.
  5 件のコメント
Saad Alqahtani
Saad Alqahtani 2021 年 7 月 6 日
This works perfect. Thank you so much!

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

その他の回答 (1 件)

Paul Hoffrichter
Paul Hoffrichter 2021 年 7 月 5 日
>> this code where it should stop as soon as one of the conditions has met
But the break just gets you out of the while loop when one of the two conditions is met. You need to add a test after the while loop to see if you broke out, in which case you can add another break do get out of the for-loop.
But within the while loop, here are your constants:
  • N (is being tested, but not changing)
  • Discard1value (is being tested, but not changing)
  • isOK1, isOK2 (are being tested but not changing)
Within the for-loop, only one variable N changes.
If the first time you hit the if N... test, you have a false condition, then you will never break out - you will be in an infinite loop since all the variables in the if test are constant within the body of the if-statement.
isOK1= (Discard2value<=target_value_30_P);
isOK2 =(Discard2value>=t2);
for N = datan(1:end)
while 1
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
break
end
end % END while
end % END for
>> it's taking so long to run and it also didn't stop utill the end
Since you are not reporting an infinite loop, then one of the conditions in the if-test must be true the first time you hit it. If the isOK test true, then it remains true for all values of N.
I advise you to put a breakpoint at the if-test, and step through a few loops to better understand your two loops.
  4 件のコメント
Saad Alqahtani
Saad Alqahtani 2021 年 7 月 6 日
Thanks for the help.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by