while loop with three conditions

10 ビュー (過去 30 日間)
sermet OGUTCU
sermet OGUTCU 2019 年 5 月 17 日
回答済み: Alex Mcaulley 2019 年 5 月 17 日
i=2;
while (sqrt(data(i,1)^2+data(i,2)^2+data(i,3)^2) > 0.10 && sqrt(data(i+1,1)^2+data(i+1,2)^2+data(i+1,3)^2) > 0.10 && sqrt(data(i+2,1)^2+data(i+2,2)^2+data(i+2,3)^2) > 0.10)
i=i+1;
end
The above three conditons in while loop working as OR operator. If one of the three conditions meet, the while loop stops. What I need is that while loop should be stopped when the three conditions are met.
  2 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 17 日
編集済み: madhan ravi 2019 年 5 月 17 日
enclose three conditions with a () separately and see if it works
sermet OGUTCU
sermet OGUTCU 2019 年 5 月 17 日
Dear @madhan, the result is same.

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

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 5 月 17 日
Changing && by ||:
i=2;
while (sqrt(data(i,1)^2+data(i,2)^2+data(i,3)^2) > 0.10 || sqrt(data(i+1,1)^2+data(i+1,2)^2+data(i+1,3)^2) > 0.10 || sqrt(data(i+2,1)^2+data(i+2,2)^2+data(i+2,3)^2) > 0.10)
i=i+1;
end
You should also add another condition to ensure that i doesn't go up to size(data,1)-2
i=2;
while ((sqrt(data(i,1)^2+data(i,2)^2+data(i,3)^2) > 0.10 || sqrt(data(i+1,1)^2+data(i+1,2)^2+data(i+1,3)^2) > 0.10 || sqrt(data(i+2,1)^2+data(i+2,2)^2+data(i+2,3)^2) > 0.10))&& i <= size(data,1)-2
i=i+1;
end

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