Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

supernoob
supernoob el 21 de Mzo. de 2018
Editada: supernoob el 21 de Mzo. de 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');

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by