I have time series data of 1 year each day 2880 values row wise so a matrix of 365X2880. I want to do monthly average. January 31 days average one file of one month, so that I can finally get 12 files each of monthly average.

Asked by Mohammed Yousuf

Mohammed Yousuf (view profile)

on 12 Sep 2019
Latest activity Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 12 Sep 2019
Accepted Answer by Andrei Bobrov

Andrei Bobrov (view profile)

I have time series data of 1 year each day 2880 values row wise so a matrix of 365X2880.
I want to do monthly average. January 31 days average one file of one month, so that I can finally get 12 files each of monthly average.
I think should be considered, days in each month are not same. I have 2015 data so it is not a leap year, Therefore february contain 28 days.
Plese help me to come out of it

Tags

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 12 Sep 2019

A - you're array 365 x 2880, in example per 2015 year.
t = (datetime(2015,1,1):datetime(2015,12,31))';
TT = array2timetable(A,'RowTimes',t);
T_out = retime(TT,'monthly','mean');

Mohammed Yousuf

Mohammed Yousuf (view profile)

on 12 Sep 2019
datetime function is not available in my Matlab version .
I am using Matlab 2014a
Andrei Bobrov

Andrei Bobrov (view profile)

on 12 Sep 2019
Well then for old MATLAB:
t = (datenum(2015,1,1):datenum(2015,12,31))';
[y,m] = datevec(t);
[a,~,c] = unique([y,m],'rows');
[ii,jj] = ndgrid(c,1:size(A,2));
out = [a, accumarray([ii(:),jj(:)],A(:),[],@mean)];
or with tables:
t = (datenum(2015,1,1):datenum(2015,12,31))';
[y,m] = datevec(t);
T = array2table([y,m,A]);
T.Properties.VariableNames = [{'Year','Month'},sprintfc('Data%d',1:size(A,2))];
T_out = varfun(@mean,T,'GroupingVariables',{'Year','Month'},'InputVariables',T.Properties.VariableNames(3:end));