how to continuously loop until a condition is met?

I have a set of data that is varying throughout (force plate data) . I am trying to find the bodyweight of the particpants as it loops through the whole script. I tried to use the mode(A) function and it worked for most but not all as the data varies between them all. So, how can i write a loop that keeps using the mode function until it finds a value that is above 400 and below 1000?

1 comentario

see the idea of a 'while' loop. you'd want something like:
value = InF;
while ~(value>400 && value<1000 );
% perform operations
value = ??? % some updated value based on the operations at this stage
end

Iniciar sesión para comentar.

Respuestas (1)

madhan ravi
madhan ravi el 5 de En. de 2021
Editada: madhan ravi el 5 de En. de 2021
value = % calculate mode;
while 1
if ((value <= 4e2) && (value <= 1e3))
break
else
% value = calculate mode again , if it doesn't satisfy
end
end

6 comentarios

how could i then find the mode and if it is not within those values, remove that mode and then redo the mode until it fits?
jessupj
jessupj el 5 de En. de 2021
Editada: jessupj el 5 de En. de 2021
why would a nested control structure with a 'break' be preferred to a simple while statement?
you could also generate an error when the criterion is met, and use a try...catch block to exit the process, but that is not preferred either.
"why would a nested control structure with a 'break' be preferred to a simple while statement?"
Who said it was preferred?
jessupj
jessupj el 6 de En. de 2021
Editada: jessupj el 6 de En. de 2021
the simplest solution that solves the problem is preferred.
if
while TRUE;
if (condition)
break
else
% stuff
end
end
and
while ~(condition);
%stuff
end
solve the same problem, the second one is PREFERRED, no?
Sometimes when a user talks about continuing to search until a condition holds, the code is more naturally of the form
while true
do some calculation
if a condition holds
break
end
do some more calculation
end
for example
while true
xnew = 1 - f(xold).^2;
if xnew > 0.99
break
end
xold = xnew;
end
Yes, the form could be rewritten as a while with a condition, but it is not always natural to do so
initialize variables so that condition is false
first_time = true;
while ~condition
if first_time
first_time = false;
else
do some more calculation
end
do some calculation
end
jessupj
jessupj el 6 de En. de 2021
i agree completely that there are instances where this would be the natural way to do it. however, i did not (and still don't... ) see anything in the OP question that indicates it is 'more natural' here.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 5 de En. de 2021

Comentada:

el 6 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by