필터 지우기
필터 지우기

How to change the value of a variable every iteration?

조회 수: 4 (최근 30일)
Stephanie Diaz
Stephanie Diaz 2017년 5월 9일
댓글: Stephanie Diaz 2017년 5월 10일
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
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?!
Stephanie Diaz
Stephanie Diaz 2017년 5월 10일
This is part of a larger script, and the reason for the two for loops is to read in "t" from a text file such that one value is read in every "i" until maxtime. I wasn't sure how else to portray this w/o two for loops, although Adam, I see what you're saying and I definitely do not want 14400 iterations!

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

답변 (0개)

카테고리

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