Effacer les filtres
Effacer les filtres

How to restart a for loop when a condition is met?

7 vues (au cours des 30 derniers jours)
AlexDp
AlexDp le 27 Sep 2019
Réponse apportée : darova le 27 Sep 2019
Hi all, I'm trying to restart a for loop when a specific condition is met. In particular this is my code:
for t=2:365;
R1=Rgas(t-tgas,mu,sigma)
ReliabilityGAS(t)=Rgas;
time(t)=t;
TIMELOST100=[ 56 64 70 120 230];
for j=1:length(TIMELOST100)
if t==(TIMELOST100(1,j))+2
tgas=t; %in this case Rgas has to be: Rgas(0,mu,sigma)
else
tgas=0; %in this case Rgas remains: Rgas(t,mu,sigma)
end
end
end
plot(time,ReliabilityGAS)
I wish every time that "tgas=t" , t in Rgas must restart from "t=2" . How can i do that?
Thanks for who will help me.

Réponses (2)

Karthi Ramachandran
Karthi Ramachandran le 27 Sep 2019
Modifié(e) : Karthi Ramachandran le 27 Sep 2019
"I wish every time that "tgas=t" " is a condition check . but you have assigened tgas = t;
if t==(TIMELOST100(1,j))+2 && tgas == t
t = 2; %in this case Rgas has to be: Rgas(0,mu,sigma)
else
might help if tgas is calculated else where

darova
darova le 27 Sep 2019
Use while loop
t = 2;
while t <= 365;
R1=Rgas(t-tgas,mu,sigma)
ReliabilityGAS(t)=Rgas;
time(t)=t;
TIMELOST100=[ 56 64 70 120 230];
tgas = 0;
t = t + 1;
for j=1:length(TIMELOST100)
if t==(TIMELOST100(1,j))+2
t = 2;
tgas=t; %in this case Rgas has to be: Rgas(0,mu,sigma)
break
end
end
end
Pay attention that your loop is infinite (t will never be bigger than 58)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by