フィルターのクリア

How to iterate odd and even runs alternatively?

11 ビュー (過去 30 日間)
Bee
Bee 2016 年 4 月 29 日
コメント済み: Elias Gule 2016 年 5 月 9 日
Hello there! I am trying to alternate between odd and even runs to do different things inside a for loop, i.e. the odd and even runs will be interdependent. For example: at t = 1, x1 = 10; at t = 2, x2 = x1+m; at t = 3, x1 = x2 ( derived from t = 2) +n and so on. The odd values of t indicate a local phase, for example and the even values of t indicate a global phase. I am trying to do this in the following way, but was wondering if there is a more efficient way to do this. Thanks for your time :)
while t < 1000
if mod(t,2)
% big block of code to calculate x using y
else
% big block of code to calculate y using x
end
end
  2 件のコメント
Roger Stafford
Roger Stafford 2016 年 4 月 29 日
You could also use a for-loop:
for k = 1:2:999
t = k;
Do block calculating x using y with t
t = k+1;
Do block calculating y using x with t
end
You don't have waste time doing 'if' tests that way.
Bee
Bee 2016 年 5 月 4 日
編集済み: Stephen23 2016 年 5 月 4 日
Hey Roger! This thing didn't come to my mind at all! It works perfectly for my purpose - simple and precise. Thanks a lot.

サインインしてコメントする。

回答 (1 件)

Elias Gule
Elias Gule 2016 年 4 月 29 日
Let's try. Define variables needed for calculation.
x = [NaN,NaN];
t = 1;
m = 2; % any value you like.
n = -2; % any value you like.
Begin processing.
while t < 100
if mod(t,2)
if t == 1
x(1) = 10;
else
x(1) = x(2) + n;
end
else
x(2) = x(1) + m;
end
t = t + 1;
end
  2 件のコメント
Bee
Bee 2016 年 5 月 4 日
Hello Elias! Thanks a lot for your suggestion, but I was looking for a simpler and precise implementation like Roger suggested above. I am too lazy to type ;)
Elias Gule
Elias Gule 2016 年 5 月 9 日
Ola Bee!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by