Looping through 3rd dimension

5 ビュー (過去 30 日間)
ellis langdon
ellis langdon 2019 年 2 月 13 日
コメント済み: Matt J 2019 年 2 月 14 日
Hi
I am currently working on a project with temperature data. The data is stored in a 128 x 64 x 2160 matrix called T.
I have calculated that summer is between index 30:48 and then +72 for the consecutive summer years.
I want to loop through the third dimension and stored this in a new variable ST.
Please see below code I have already made, i'm not too sure where to go after this.
ST=NaN*ones(128,64,30) %Setting up grid
x=0;
for i=30:48;
x=x+1;
ST(:,:,x) = T(:,:,[i:i]); %Looping through third dimension 30:48
end
ST should essentially be 128 x 64 x 30 or 128 x 64 x 540 matrix
Any help would be great, thank you!
  3 件のコメント
ellis langdon
ellis langdon 2019 年 2 月 13 日
Sorry, it should actually have 18 elements so the final grid should be 128 x 64 x 540. However, if I modify the code for the ammount of elements I need, should that work?
Thank you
Matt J
Matt J 2019 年 2 月 13 日
Yes.

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

採用された回答

Jan
Jan 2019 年 2 月 13 日
編集済み: Jan 2019 年 2 月 14 日
Your loop has 19 iterations. It is not clear, why you expect that the output has a size of 30 or 540 in the 3rd dimension.
[i:i] is the same as the much simpler: i .
A simplified version of your code:
ST = T(:, :, 30:48)
No loop needed. But I do not see, how this could produce a size of 30 or 540.
By the way, this can be simplified also:
ST=NaN*ones(128,64,30)
% Better:
ST = NaN(128, 64, 30)
But it is not useful here, so better omit it.
[EDITED] After the clarifications:
ST = NaN(128, 64, 18, 30); % Pre-allocate
ini = 30;
len = 18;
for k = 1:30
ST(:, :, :, k) = T(:, :, ini:ini + len - 1);
ini = ini + 72;
end
ST = reshape(ST, 128, 64, 540);
% Or:
v = 1:72;
match = (30 <= v & v < 48);
ST = T(:, :, repmat(match, 1, 30));
  2 件のコメント
ellis langdon
ellis langdon 2019 年 2 月 13 日
編集済み: Jan 2019 年 2 月 14 日
I am trying to put all of the summer values into the new grid (ST). The summer values are between 30 and 48, which increase annually by 72.
For example the loop will put all the values between 30:48 into ST(:,:,x), after the loop has reached 48, it should go back and increase everyhing by 72 until the ST array has been filled.
I hope that makes sense.
Matt J
Matt J 2019 年 2 月 14 日
ellis langdon commented:
Thank you Jan, that seemed to do the trick! :)

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

その他の回答 (1 件)

Matt J
Matt J 2019 年 2 月 13 日
編集済み: Matt J 2019 年 2 月 13 日
ST=reshape( T, 128 ,64,72,30);
ST=reshape( ST(:,:,30:48,:), 128, 64,[]);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by