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

7 次查看(过去 30 天)
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.

回答(2 个)

Karthi Ramachandran
编辑:Karthi Ramachandran 2019-9-27
"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 2019-9-27
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by