How to average a time series with different length by days?
1 view (last 30 days)
Show older comments
Hi.
I want to average all the hours from one day into one. So that all the values from one day is averaged together. Any suggestions would be of great help!
- Kristine
My time series looks like this:
Year Month Day Hour Minute Second PM2.5
2009 5 4 22 0 0 15
2009 5 4 23 0 0 20
2009 5 5 0 0 0 16
2009 5 5 1 0 0 16
2009 5 5 2 0 0 16
2009 5 5 3 0 0 16
2009 5 5 4 0 0 17
2009 5 5 5 0 0 17
2009 5 5 6 0 0 21
2009 5 5 7 0 0 17
2009 5 5 8 0 0 20
2009 5 5 9 0 0 18
2009 5 7 15 0 0 13
2009 5 7 16 0 0 17
2009 5 7 17 0 0 18
1 Comment
Jon
on 27 Jul 2015
Edited: Jon
on 27 Jul 2015
Not sure what PM2.5 is, but you can convert your dates to datenumbers (see http://www.mathworks.com/help/matlab/ref/datenum.html) and then do the averaging.
DateNumber = datenum(Y,M,D,H,MN,S)
You can use a for loop to find where the day changes and do the averaging within that window. You can also use indexing and logical statements to avoid the for loop.
Accepted Answer
Walter Roberson
on 27 Jul 2015
Assuming your data is named TS
[unique_rows, ~, idx] = unique(TS(:,1:4),'rows');
daily_av = accumarray(idx(:), TS(:,7), [], @mean);
avg_TS = [unique_rows, daily_av(:)];
3 Comments
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!