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

6 次查看(过去 30 天)
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!

采纳的回答

supernoob
supernoob 2018-3-21
编辑:supernoob 2018-3-21
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');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by