How to change the value of a variable every iteration?
3 次查看(过去 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!