hourly mean for my data?
2 views (last 30 days)
Show older comments
TV Reddy Gireesh
on 11 Aug 2017
Commented: TV Reddy Gireesh
on 31 Aug 2017
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
Accepted Answer
Jan
on 11 Aug 2017
Edited: Jan
on 11 Aug 2017
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.
For accumarray see: https://www.mathworks.com/matlabcentral/answers/352434-finding-sum-of-large-array-based-on-row-values
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.
More Answers (0)
See Also
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!