필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
AlexDp
AlexDp 2019년 9월 27일
답변: darova 2019년 9월 27일
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일
편집: 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