While loop with all elements meeting the conditions

How do I set a while loop that runs until all elements meet the condition? For instance:
t = -100:200;
F = t;
indices = 1:100;
while F(indices) < 0 %
indices = indices + 1;
end
This while-loop only will run, until one of the elements of F(indices) will be creater than 0. However I wish it will run until all of the elements will meet the condition. How to implement this?

 採用された回答

Davide Masiello
Davide Masiello 2022 年 10 月 28 日
編集済み: Davide Masiello 2022 年 10 月 28 日

1 投票

t = -100:200;
F = t;
indices = 1:100;
while all(F(indices)<0)
indices = indices + 1;
end

4 件のコメント

Niklas Kurz
Niklas Kurz 2022 年 10 月 28 日
I also thought of it but apparently this is not working because it also stops as soon as one component is greater zero. If it was working right, indices should be an array from 101:201 actually.
Torsten
Torsten 2022 年 10 月 28 日
編集済み: Torsten 2022 年 10 月 28 日
Then you must code
t = -100:200;
F = t;
indices = 1:100;
while any(F(indices)<0)
indices = indices + 1;
end
Then the while loop continues until all elements of F(indices) are >=0.
Davide Masiello
Davide Masiello 2022 年 10 月 28 日
Yes, @Torsten is correct, I did not properly understand the question before.
Niklas Kurz
Niklas Kurz 2022 年 10 月 28 日
Awesome, thank you so much!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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