フィルターのクリア

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 Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by