how to average seasonly?

3 views (last 30 days)
sky walker
sky walker on 6 Mar 2021
Edited: dpb on 6 Mar 2021
i have data set 141x49x300 (longitude,latitude,time), it contains data monthly average from 1995-2019. which mean 141x49x1 mean jan 1995 and 141x49x300 mean december 2019.
how do make average data every three month (dec, jan, feb) (mar, apr, may) and so on?
thanks

Answers (1)

dpb
dpb on 6 Mar 2021
Edited: dpb on 6 Mar 2021
This one is cute... :) Unfortunately, timetable won't accept a 2D array by row so have to do things directly...
t=linspace(datetime(1995,1,1),datetime(1995,12,31),300).'; % 300 elements between above start, stop
[r,c,p]=size(A); % sizes of data, A
M=(splitapply(@mean,reshape(A,r*c,[]).',findgroups(quarter(t)))); % compute means by quarter by columns
M=reshape(M.',r,c,[]); % reshape back to rxcx4
Substitute your real time vector for t, of course.
I did a trial here to check ordering...
t=linspace(datetime(1995,1,1),datetime(1995,12,31)).'; % only 100 instead of 300
A=randi([50 100],5,5,100); % dummy 5x5 data
M=(splitapply(@mean,reshape(A,25,100).',findgroups(quarter(t)))); % compute means 2D
>> M(:,1:3) % small piece to look at to check
ans =
78.4400 76.6000 75.3200
76.3200 73.2400 74.7200
71.6800 76.7200 83.7200
80.6000 74.3600 74.8000
>>
>> MM=reshape(M.',5,5,4);
>> whos MM
Name Size Bytes Class Attributes
MM 5x5x4 800 double
>> squeeze(MM(1,1,:))
ans =
78.44
76.32
71.68
80.60
>> squeeze(MM(2,1,:))
ans =
76.60
73.24
76.72
74.36
>>

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!