필터 지우기
필터 지우기

Multiple conditional statements and a for loop.

조회 수: 268 (최근 30일)
nate vaniderstine
nate vaniderstine 2019년 1월 30일
답변: Bob Thompson 2019년 2월 7일
i have a for loop that calls a function and the if statements are supposed to determine when the function gets within a certain range, byut i am unsure if i need the statements to be nested within each other it to split them up. heres some of the code:
for i = 1:npoints
t(i)=(i-1)*tmax/npoints;
x(i) = sec_ord(wn, zeta, t(i));
end
if sec_ord(wn, zeta, t(i))<1.02
if sec_ord(wn, zeta, t(i))>0.98
if flag==1
t=t+inc;
elseif flag==0
flag=1;
tss=t;
t=t+inc;
if t<=tmax
sec_ord(wn, zeta, t(i));
elseif flag==1
tss=set_time;
end
end
else
flag=0;
t=t+inc;
end
end
after this runs i want to be able to use some of the values(such as tss and set_time) set inside the if statements but they do not get stored.
Any help would be appreciated,
thanks.
  댓글 수: 1
Harshit Jain
Harshit Jain 2019년 2월 7일
Just wanted to ask if the "if" statement is even working, since it is outside the "for" loop and t(i) would give error there. Also, I would appreciate if you would elaborate the question a little bit more.

댓글을 달려면 로그인하십시오.

채택된 답변

Bob Thompson
Bob Thompson 2019년 2월 7일
You can combine multiple conditions using & (and) and | (or) (&& and || are also things). Using this method I think it's worth combining the first two levels of your if statement.
if sec_ord(wn, zeta, t(i))<1.02 & sec_ord(wn, zeta, t(i))>0.98
For the flag condition I would suggest leaving it nested because you have multiple conditions you're considering, and I find it slightly easier to comprehend when considered separate from the range condition.
Because you are calling t(i) within the if statements they will need to be nested inside for loop for a unique check each time you run the loop. As your code is currently written the t(i) within the if statements will simply be evaluated as t(npoints).
after this runs i want to be able to use some of the values(such as tss and set_time) set inside the if statements but they do not get stored.
If by 'not get stored' you mean that you only receive a single output value there are two reasons for this. The first is because the if statement is outside the for loop, and so only gets evaluated once, as already mentioned. The second is because you have not indexed tss or set_time, so the variables will simply contain a single value that gets overwritten each time they are calculated.
If any of these are not the answers you were looking for please feel free to expand on your questions.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by