while loop for conditional statement
1 次查看(过去 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.
1 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!