Borrar filtros
Borrar filtros

How to change the value of a variable every iteration?

4 visualizaciones (últimos 30 días)
Stephanie Diaz
Stephanie Diaz el 9 de Mayo de 2017
Comentada: Stephanie Diaz el 10 de Mayo de 2017
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 comentarios
Adam
Adam el 10 de Mayo de 2017
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 el 10 de Mayo de 2017
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!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by