How can I change (or not change) the value of a variable each iteration?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I am attempting to either change the value of a variable each iteration, or keep the value the same, depending on the circumstances. I have a loop of 24 iterations,where I am selecting elements from a matrix (X) according to a set of criteria. It is set up something like the following:
X=matrix
xpos=100 %initial x- position
ypos=100 %initial y- position
for t=1:24
hour = t
trip_duration = t
for ii=1:10
for jj=1:10
if X(xpos+ii,ypos+jj) = 0
trip_duration = 1
end
end
end
end
Basically, the variable "trip_duration" resets to "1" every time an element with a value of "0" is found. The idea is for trip duration to then be "2" in the next iteration. If no element with a value of 0 is found, then trip-duration is simply "t". So, at any point in the loop, trip_duration and t can have different values, or the same. I've tried several ways to represent this, but am not successful in having trip_duration increase from it's previous value. For example, I can change the value to "1" during an iteration, and instead of changing to "2" during the next iteration, it will be the value of "t". I've simplified the code a good amount below so please let me know if what I am asking is not clear. Any help is appreciated!
9 commentaires
Santhana Raj
le 31 Mai 2017
does this work for you?
for t=1:24
hour = t;
if any( any( ~X(xpos+1:xpos+10, ypos+1:ypos+10) ) )
trip_duration=0;
else
trip_duration=t;
end
end
Walter Roberson
le 31 Mai 2017
The trip_duration=0 should be trip_duration=1 for consistency with the problem requirements.
Réponses (0)
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!