필터 지우기
필터 지우기

How can I change (or not change) the value of a variable each iteration?

조회 수: 1 (최근 30일)
Stephanie Diaz
Stephanie Diaz 2017년 5월 26일
댓글: Walter Roberson 2017년 5월 31일
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
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
Walter Roberson 2017년 5월 31일
The trip_duration=0 should be trip_duration=1 for consistency with the problem requirements.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by