Clear Filters
Clear Filters

Computing average times from an array of datetime using only selected rows

3 views (last 30 days)
Hi there, I have an array of datetime variables, and an array with a number in each row which corresponds to how I want to 'group' the datetime array. For example, my datetime array might look like:
>>exact_times =
8×1 datetime array
21-Jan-2016 11:24:17
21-Mar-2018 11:24:17
21-Mar-2018 11:24:17
21-Mar-2018 11:24:17
21-Mar-2018 11:25:36
21-Mar-2018 11:25:36
21-Mar-2018 11:25:47
21-Mar-2018 11:25:47
and my subs array might look like
>> subs
subs =
2
1
1
2
1
4
4
4
1
3
I want to average all the rows in exact_times which correspond to a given number in subs, ie all the rows which correspond to 1 in subs (this is rows 2,3,5,9) and then do the same for 2 in subs, etc.
If I could use accumarray I would use something like: average_time = accumarray(subs, exact_times, [], @mean); but I can't use that on datetime variables. I can't change the ordering of anything in either array for other reasons. Can anyone help me out?
Thanks in advance!

Accepted Answer

supernoob
supernoob on 21 Mar 2018
Edited: supernoob on 21 Mar 2018
I figured it out: simply convert to datenum and then back:
average_time = accumarray(subs, datenum(exact_times), [], @mean); avg_data.average_time = datetime(average_time, 'ConvertFrom', 'datenum');

More Answers (0)

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!