while loop for conditional statement
1 view (last 30 days)
Show older comments
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 Comment
Accepted Answer
Jan
on 5 Apr 2022
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 Comments
Jan
on 6 Apr 2022
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.
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!