Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

supernoob
supernoob le 21 Mar 2018
Modifié(e) : supernoob le 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');

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by