MATLAB: Hourly mean for the data

daily meanhourly meanhourly mean for one day data with 5min intervalMATLABMATLAB and Simulink Student Suite

hello sir, im new to matlb, i have a data set like datenum(or)datevec + data in one matrics for example
7.3685e+005 656 756 ....
7.3685e+006 621 736....
.
.
(or)
2017 05 1 656 756 ....
2017 05 1 621 736....
.
.
like that for 24 hrs and with 5min interval i want hourly mean data, and i have tried unique command and others to filter it's not coming properly, note: some time data gap will be there but in file it will continued like 00:50 13:30 gap is there like almost 13 hours, please help out this, thanking you,
thanks in advance

Best Answer

It is easier with the datenum format. Start with defining hours:
Time = data(:, 1); % Hop this matches your data
Hour = floor(Time * (24 * 60));
[uH, iH, iuH] = unique(Hour);
Now splitapply or accumarray will help, but I suggest a dull loop at first:
MeanData = zeros(numel(uH), size(data, 2) - 1);
for k = 1:numel(uH)
MeanData(k, :) = mean(data(iuH == k, 2:end), 1);
end
With modern functions:
MeanData = splitapply(@myMean1, data(:, 2:end), iuH);
function m = myMean1(x)
m = sum(x, 1) / size(x, 1);
I used an external function myMean1 instead of simply @mean, because mean() would operate over the 2nd dimension, if only one value is existing for a certain hour. An anonymous function would do the trick also, but slower.
By the way: If you have a modern Matlab version, take a look to the new datetime class.
Note: The code is untested due to the lack of input data.