Yearly average from monthly data

10 次查看(过去 30 天)
Hello,
It has been a while since I have made any new loops that I have not copied from old scripts, so I am a little out of practice. This question should be fairly easy for most to answer.
I have a data set with monthly data:
Size(A)=192,144,120
I would like to put this into ten, yearly averages:
Size(B)=192,144,10
I have just done it the long way around-
B(:,:,1)=mean(A(:,:,1:12),3);
B(:,:,2)=mean(A(:,:,13:24),3);
B(:,:,3)=mean(A(:,:,25:36),3);
B(:,:,4)=mean(A(:,:,37:48),3);
B(:,:,5)=mean(A(:,:,49:60),3);
B(:,:,6)=mean(A(:,:,61:72),3);
B(:,:,7)=mean(A(:,:,73:84),3);
B(:,:,8)=mean(A(:,:,85:96),3);
B(:,:,9)=mean(A(:,:,97:108),3);
B(:,:,10)=mean(A(:,:,109:120),3);
This works fine, but is a little tedious and I will be wanting to do this with much larger data sets in the coming months.
What is the best way to do this in a for loop?
Thank you!
  2 个评论
Dennis
Dennis 2018-11-29
Is there a specific reason for a loop? I think reshape should work aswell:
B=mean(reshape(A,192,144,12,[]),4);
HA
HA 2018-11-29
This does not quite give me what I want.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2018-11-29
s = size(A);
B = squeeze(mean(reshape(A,[s(1:2),12,s(3)/12]),3));
  2 个评论
HA
HA 2018-11-29
This Works well, thank you!
Marek Harenda
Marek Harenda 2022-4-1
Hello sir, can you help me with changing this code into calulation of the monthly avarage by which i mean calculating January, February, etc. mean from the whole dataset? I am asking you, because your code for annual mean is working with my data so I hope that I only need to do some little change to achieve my goal. I know that there are topics more related to my problem, but the proposals for solving it turned out not to be satisfactory for me. I see that in this case we need only to extract 12 consecutive months for each year, but in my case I probably need to extract every 12 month with 1 month shift so it is a bit more complicated for me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by