Effacer les filtres
Effacer les filtres

Yearly average from monthly data

6 vues (au cours des 30 derniers jours)
HA
HA le 29 Nov 2018
Commenté : Marek Harenda le 1 Avr 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 commentaires
Dennis
Dennis le 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 le 29 Nov 2018
This does not quite give me what I want.

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by