while loop for conditional statement

1 回表示 (過去 30 日間)
sermet OGUTCU
sermet OGUTCU 2022 年 4 月 5 日
コメント済み: Jan 2022 年 4 月 6 日
array_data=33 x 1 % double
window_size=10;
m_BW=0.5;
var_BW=0.5;
for i=1:window_size
m_BW(i+1)=(i/(i+1))*m_BW(i) + (1/(i+1))*BW(i+1);
var_BW(i+1)= (i/(i+1))*var_BW(i) + (1/(i+1))*((BW(i+1)-m_BW(i))^2);
if abs (BW(i+1)-m_BW(i)) >= sqrt(var_BW(i)) && abs (BW(i+2)-BW(i+1)) <= (1*lambda_wl)
slip(i)=i;
end
end
if this "if" statement is true, for example at i=5, then for loop needs to rebuild as:
for i=(5+1):(5+window_size)
if this "if" statement is not true, then for loop needs to continue as incremented by 10 as follows:
for i=(10+1):(10+window_size)
for loop needs to continue until all 10 batches within 33 x 1 array_data is completed. How I can write a compact code for the above statements regardless of the size of the array_data for saving all slip(i) data?
For another example, assuming that "if" statement is true for the first time at i=16 (for i= (10+1):(10+window_size) ), then, for i= (16+1):(16+window_size) needs to run for the last time within 33 x 1 data.
  1 件のコメント
MJFcoNaN
MJFcoNaN 2022 年 4 月 5 日
Why don't you directly loop from 1 to a large number, such as 26?

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

採用された回答

Jan
Jan 2022 年 4 月 5 日
Maybe:
array_data=33 x 1 % double
window_size=10;
m_BW=0.5;
var_BW=0.5;
i = 1;
fin = window_size;
while i <= fin
m_BW(i+1) = i/(i+1) * m_BW(i) + (1/(i+1)) * BW(i+1);
var_BW(i+1) = i/(i+1) * var_BW(i) + (1/(i+1)) * (BW(i+1)-m_BW(i))^2;
if abs(BW(i+1) - m_BW(i)) >= sqrt(var_BW(i)) && abs(BW(i+2)-BW(i+1)) <= (1*lambda_wl)
slip(i) = i;
fin = i + window_size;
end
i = i + 1;
end
  2 件のコメント
sermet OGUTCU
sermet OGUTCU 2022 年 4 月 5 日
編集済み: sermet OGUTCU 2022 年 4 月 5 日
Dear @Jan, I run the code, but when the if condition becomes true the loop stops. If the "if " condition is true, the loop needs to goes on, but in your code, it seems that it stops. I stated this part in my question as follows:
if this "if" statement is true, for example at i=5, then for loop needs to rebuild as:
for i=(5+1):(5+window_size)
Jan
Jan 2022 年 4 月 6 日
Yes, this is exactly what my code does: If redefines the value of fin, such that the loop goes on.
But maybe I do not really understand, what you are asking for. At least my code could show you how to use a while loop. This allows to change the limits during the loop is running.

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

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