while loop for conditional statement

3 次查看(过去 30 天)
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.

采纳的回答

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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by