How to change the value of a variable every iteration?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi all,
I want to change the value of 'trip_duration' every iteration. I have the following code:
maxtime=120
threshold_temperature=30
for i=1:maxtime
trip_duration=i
for t=1:120; %iteration over all values 1-120
current_temperature =temperature_hourly(t,:); %where 'temperature_hourly' reads in one value from a text file until the 120th value
if current_temperature <= threshold_temperature
Influenceof_temp_current= 1;
else
Influenceof_temp_current = current_temperature/threshold_temperature;
end
time_since_last_water = trip_duration*(Influenceof_temp_current) ;
prob_drinking = 1/(1+exp(-.05*(time_since_last_water-10)));
rn=rand;
if prob_drinking>rn
k=1;
else
k=2;
end
end
end
The issue that I am having is that trip_duration does not change with every iteration, where my intention is for trip_duration to be 1 when i=1, 2 when i=2, etc. Consequently, the outputs for 'prob_drinking'are not correct. The error that I am receiving is "Attempted to access trip_duration(2); index out of bounds because numel(trip_duration)=1." If I simply run the first "for" loop, 'trip_duration' changes as I need it to, but not if I run the whole code (where it is always 120). Any ideas as to why this is occuring?
Thanks in advance
댓글 수: 6
Adam
2017년 5월 10일
You don't appear to be storing or outputting any results from either loop so what exactly is the expected final result of all this?
You say trip_duration is always 120, but when is 'always'? How are you determining what it's value is? Are you stepping through a breakpoint 14400 times?!
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!