How can I change (or not change) the value of a variable each iteration?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Santhana Raj
2017-5-31
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
2017-5-31
The trip_duration=0 should be trip_duration=1 for consistency with the problem requirements.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!