Filter löschen
Filter löschen

while loop for conditional statement

1 Ansicht (letzte 30 Tage)
sermet OGUTCU
sermet OGUTCU am 5 Apr. 2022
Kommentiert: Jan am 6 Apr. 2022
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 Kommentar
MJFcoNaN
MJFcoNaN am 5 Apr. 2022
Why don't you directly loop from 1 to a large number, such as 26?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 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 Kommentare
sermet OGUTCU
sermet OGUTCU am 5 Apr. 2022
Bearbeitet: sermet OGUTCU am 5 Apr. 2022
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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by