MATLAB: Calculate longterm daily average

MATLAB

I manged to do monthly average like this.
mat = randi([0 14600], 730, 5, 2);
sc1 = (datetime(1981,1,1):datetime(1982,12,31))';
splitmat = num2cell(permute(mat, [1 3 2]), [1 2]);
TT1 = timetable(sc1,splitmat{:});
monthlyaverage = retime(TT1, 'monthly', 'mean');
If I do
monthlyaverage = retime(TT1, 'daily', 'mean');
it will return the same value as the data is daily. However, I am interested to get longterm daily average that means average of day of the year (day 1 … day 365). The final answer number of rows will be only 365. is there similar approach that is concise like retime to do it?
thanks

Best Answer

Create timetable (from OP's code)
mat = randi([0 14600], 730, 5, 2);
sc1 = (datetime(1981,1,1):datetime(1982,12,31))';
splitmat = num2cell(permute(mat, [1 3 2]), [1 2]);
TT1 = timetable(sc1,splitmat{:});
Compute averages for day-of-year using grpstats().
% List all data columns
varList = TT1.Properties.VariableNames(2:end);
% Compute day-of-year and add it to the table
TT1.DOY = day(TT1.sc1, 'dayofyear');
% Compute mean for each day-of-year
% This requires converting the TimeTable to a Table
% This also requires Stats & Machine Learning Toolbox
dailyAvg = grpstats(timetable2table(TT1(:,2:end)),'DOY','mean','DataVars',varList)
First few rows of output
head(dailyAvg)
DOY GroupCount mean_Var2 mean_Var3 mean_Var4 mean_Var5
___ __________ ________________ ________________ ________________ ________________
1 1 2 6272 2320.5 9136.5 10590 5050.5 10498 8312.5 9643
2 2 2 12622 987.5 6968 4419 2484 7055.5 11215 9381.5
3 3 2 6032 1531 3696 1633.5 11304 8133 10159 7516
4 4 2 9188 1711 1905 7979 10044 3948 7382 6632
5 5 2 8311 10974 9007 11422 7507 3849.5 3333 6290.5
6 6 2 14211 4030.5 10858 4806.5 8750 7859 4807 3746
7 7 2 9090.5 5260 5890.5 9528 11296 4518.5 7327.5 10649
8 8 2 7022.5 11904 3766 12161 12543 8624.5 5090 10506