MATLAB: How i do weekly average

averagemeanweek

Hi all,
I have a matrix of 978*744 which is = no. observation*hours in a month. and the attached mat file is the DateTime vector. I want to make a weekly average.
Thank you in advance.

Best Answer

Let data - your array [978 x 744].
For MATLAB >= R2016b (here week -> [Sun : Sat]):
Time = (datetime([2017 7 1 0 0 0]):hours(1):datetime([2017 7 31 23 0 0]))';
% or: Time = datetime(timeCopy);
TT = array2timetable(data','R',Time);
TT_out = retime(TT,'weekly','mean');
For MATLAB < R2016b ( here week -> [Mon : Sun]):
data = data';
Time = (datetime([2017 7 1 0 0 0]):hours(1):datetime([2017 7 31 23 0 0]))';
wd = rem(weekday(Time) + 5, 7) + 1;
ii = [true;diff(wd == 1) == 1];
g = cumsum(ii);
[r,c] = ndgrid(g,1:size(TT,2));
TT_out = [table(Time(ii),'V',{'Time'}),...
array2table(accumarray([r(:),c(:)],data(:),[],@mean))];
Related Question