Loop iteration with condition met

3 visualizaciones (últimos 30 días)
Johan Johan
Johan Johan el 24 de Nov. de 2019
Comentada: Johan Johan el 24 de Nov. de 2019
I have this program in matlab
close
clc
%%%%%%%%%%%%%%%%
Tol = 0.5;ma=10;t=[1 2];B=[];loopEnd=3; myData = zeros(ma,loopEnd);
%while loop execution
for a=1:ma
x=rand()
v=sum(x*t);
if v<Tol
return
end
B=[B,v]
end
How can i add condition (if statment) ,the condition is :
1-if the result is big than 1.5 cancel it and biging new iteration,
2-if the result is less than 0.5 save it in B, if not repeat new iteration (new loop) ,the loop is repeat until the final vector have 10 values.

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 24 de Nov. de 2019
Tol=1.5;ma=10;
t=[1 2];
B=[];
for a=1:ma
x=rand();
result=sum(x*t);
while result>Tol || result<0.5
x=rand()
result=sum(x*t);
B(a)=result;
end
end
B
  1 comentario
Johan Johan
Johan Johan el 24 de Nov. de 2019
Thank you,but i have other question,If i want sub loop for example ma=5 if the condition is not met i cancel the loop and starting new loop.
Why , if using optimization method, sometimes the first iterations such as 10 iterations don't met the better solution .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by