필터 지우기
필터 지우기

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

조회 수: 3 (최근 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