How to modify a variable inside of a loop and use it outside the loop

I'm writing an algorithm right now, and I'm having trouble with my variables. For instance, the variable values modified inside of a loop are not being saved when I exit that loop. How do I get the correct number set as that variable? For example, in this loop the est_trains_per_hr is still acting as 0 after the loop
est_trains_per_hr = 0 ;
for time_num = 1 : length(times)
time = times(time_num) ;
if time >= 800 && time < 900
est_trains_per_hr = est_trains_per_hr + 1 ;
end
end

답변 (1개)

What does this show:
sum(times(:)>=800 & times(:)<900)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2017a

질문:

2019년 3월 25일

답변:

2019년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by