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

3 views (last 30 days)
AlexDp
AlexDp on 27 Sep 2019
Answered: darova on 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.

Answers (2)

Karthi Ramachandran
Karthi Ramachandran on 27 Sep 2019
Edited: Karthi Ramachandran on 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 on 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)

Categories

Find more on Loops and Conditional Statements 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!