Azzera filtri
Azzera filtri

Yearly average from monthly data

6 visualizzazioni (ultimi 30 giorni)
HA
HA il 29 Nov 2018
Commentato: Marek Harenda il 1 Apr 2022
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 Commenti
Dennis
Dennis il 29 Nov 2018
Is there a specific reason for a loop? I think reshape should work aswell:
B=mean(reshape(A,192,144,12,[]),4);
HA
HA il 29 Nov 2018
This does not quite give me what I want.

Accedi per commentare.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 29 Nov 2018
s = size(A);
B = squeeze(mean(reshape(A,[s(1:2),12,s(3)/12]),3));
  2 Commenti
HA
HA il 29 Nov 2018
This Works well, thank you!
Marek Harenda
Marek Harenda il 1 Apr 2022
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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by