# 24 hour running mean

8 views (last 30 days)
Lilya on 13 Jun 2020
Commented: Lilya on 14 Jun 2020
Hi all-
I have data that have been collected at the following time (please see below). The time is not uniform, so what I need is to apply the moving average of 24 hours for the data and the corresponding time.
2019 1 31 11 47 17.0302999999985
2019 1 31 17 20 33.0175000000017
2019 1 31 21 20 56.8234000000084
2019 2 1 1 4 20.7022000000002
2019 2 1 5 29 43.4082000000017
2019 2 1 9 57 34.1555000000008
2019 2 1 15 11 28.3795000000027
2019 2 1 21 27 38.1998000000021
2019 2 2 4 40 0.397300000000541
2019 2 2 11 37 42.8656000000046
2019 2 2 17 9 45.1759999999995
2019 2 2 21 30 22.4748000000109
2019 2 3 1 30 46.7392000000000
2019 2 3 5 26 0.550600000002305
2019 2 3 8 33 12.1473000000005
2019 2 3 11 44 14.3238000000056
2019 2 3 15 4 14.8778999999995
2019 2 3 18 15 16.7223000000085
2019 2 3 21 19 5.61710000000312
2019 2 4 0 10 14.2442000000000
2019 2 4 2 38 44.0331000000006
2019 2 4 5 2 35.0192000000025
2019 2 4 7 21 3.47360000000117
2019 2 4 9 29 47.9219999999987
2019 2 4 11 39 46.4229000000050
2019 2 5 6 38 51.4264000000003
2019 2 5 12 21 3.65039999999863
2019 2 5 18 7 59.8438000000024
2019 2 5 21 12 20.0656000000017

Ameer Hamza on 13 Jun 2020
I guess you want to calculate average for each day. Try this
M = [
2019 1 31 11 47 17.0302999999985
2019 1 31 17 20 33.0175000000017
2019 1 31 21 20 56.8234000000084
2019 2 1 1 4 20.7022000000002
2019 2 1 5 29 43.4082000000017
2019 2 1 9 57 34.1555000000008
2019 2 1 15 11 28.3795000000027
2019 2 1 21 27 38.1998000000021
2019 2 2 4 40 0.397300000000541
2019 2 2 11 37 42.8656000000046
2019 2 2 17 9 45.1759999999995
2019 2 2 21 30 22.4748000000109
2019 2 3 1 30 46.7392000000000
2019 2 3 5 26 0.550600000002305
2019 2 3 8 33 12.1473000000005
2019 2 3 11 44 14.3238000000056
2019 2 3 15 4 14.8778999999995
2019 2 3 18 15 16.7223000000085
2019 2 3 21 19 5.61710000000312
2019 2 4 0 10 14.2442000000000
2019 2 4 2 38 44.0331000000006
2019 2 4 5 2 35.0192000000025
2019 2 4 7 21 3.47360000000117
2019 2 4 9 29 47.9219999999987
2019 2 4 11 39 46.4229000000050
2019 2 5 6 38 51.4264000000003
2019 2 5 12 21 3.65039999999863
2019 2 5 18 7 59.8438000000024
2019 2 5 21 12 20.0656000000017];
[idx, y, m, d] = findgroups(M(:,1), M(:,2), M(:,3));
avg_val = accumarray(idx, M(:,6), [], @mean);
M_avg = [y m d avg_val];
Also see retime() function for an alternate solution.
Ameer Hamza on 13 Jun 2020
I am glad to be of help!

Steven Lord on 13 Jun 2020
Convert your data into a datetime array. Call movmean with your data as the first input, hours(24) as the window, and your datetime array as the SamplePoints. See the "Sample Points for Moving Average" example on the movmean documentation page.
Lilya on 14 Jun 2020
Thank you, Steven, for the suggestion as it takes into consideration the time as it is.
It works successfully!! (please see the attachment)
much appreciated.