For loop confusion!

1 回表示 (過去 30 日間)
Sophia
Sophia 2016 年 5 月 12 日
編集済み: Chad Greene 2016 年 5 月 13 日
ind1 = 1:6;
for t2=1:size(mm_r,3);
if t2 <= 6
wt_r = mm_r(1:361,1:361,ind1);
wint2_r(:,:,t2) = mean(wt_r,3); %wint2_r is of 361*361*6
ind1 = ind1+6;
else
end
end
To calculate the sequential differences I want to write this part in for loop, but it is not working out the way i want lets say an_r of size 361*361*5
anom_90_84_r = wint2_r(:,:,2) - wint2_r(:,:,1);
anom_96_90_r = wint2_r(:,:,3) - wint2_r(:,:,2);
anom_02_96_r = wint2_r(:,:,4) - wint2_r(:,:,3);
anom_08_02_r = wint2_r(:,:,5) - wint2_r(:,:,4);
anom_15_08_r = wint2_r(:,:,6) - wint2_r(:,:,5);
  3 件のコメント
dpb
dpb 2016 年 5 月 13 日
Don't know that we can help simply on the basis of "it isn't working the way I want", sorry. What is the desired output from what input? Generally a (very small) sample case that illustrates numerically helps clarify what often is difficult describing solely in words.
Weird Rando
Weird Rando 2016 年 5 月 13 日
編集済み: Weird Rando 2016 年 5 月 13 日
Just letting you know that you didn't really need the if statement if you just start the for loop at 2.
for k = 2:size(wint2_r,3)
anom_r = wint2_r(:,:,k)-wint2_r(:,:,(k-1));
an_r(:,:,k-1) = anom_r;
end
Basically, are you asking how to dynamically name variable to store data. So in the for loop you would do this
anom_90_84_r = wint2_r(:,:,2) - wint2_r(:,:,1);
anom_96_90_r = wint2_r(:,:,3) - wint2_r(:,:,2);
anom_02_96_r = wint2_r(:,:,4) - wint2_r(:,:,3);
anom_08_02_r = wint2_r(:,:,5) - wint2_r(:,:,4);
anom_15_08_r = wint2_r(:,:,6) - wint2_r(:,:,5);

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

採用された回答

Chad Greene
Chad Greene 2016 年 5 月 13 日
編集済み: Chad Greene 2016 年 5 月 13 日
I think you can skip the loops and use diff:
an_r = diff(wint2_r,1,3);

その他の回答 (0 件)

カテゴリ

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