Average/mean of datetime hours and minutes ITSELF - not corresponding embedded data

20 次查看(过去 30 天)
How to I compute the mean/average time of a datetime array containing hours and minutes?
rowMin =
20×1 datetime array
09:00
09:10
09:00
09:14
09:59
09:05
09:00
09:35
13:15
09:00
09:00
15:00
09:35
10:15
14:10
16:35
09:00
11:15
09:00
10:20
How to I compute the mean/average time?
rowMin.Format='HH:mm'
mean(rowMin)
Does not work. Neither converting to table and subsequently timetable does:
rowMinTable=table(rowMin)
rowMinTimetable=table2timetable(rowMinTable)
dt = minutes(30);
output = retime(table2timetable(rowMinTable),'regular','linear','TimeStep',dt)
Also, converting to datenum to compute mean and then revert to datetime is not the solution, since it uses the dates as well (I am ONLY interested in the mean of the hours and minutes HH:MM):
rowMin.Format='HH:mm';
rowMinDatenum=datenum(rowMin)
rowMinDatenumMean=mean(rowMinDatenum)
rowMinMean=datetime(rowMinDatenumMean,'ConvertFrom','datenum')
This question is also asked on stack exchange:
https://stackoverflow.com/questions/65324040/average-mean-of-datetime-hours-and-minutes-itself-not-corresponding-embedded-d

采纳的回答

Cris LaPierre
Cris LaPierre 2020-12-17
Changing the format does not change the underlying data. It just changes how it appears.
I think you would have to convert your datetimes to durations by subtracting the date, and compute the mean duration. Do the times all come from the same day?
  3 个评论
Cris LaPierre
Cris LaPierre 2020-12-17
Ok, there are a few ways I can think of to do this. They share the same thing - you must extract the time component, then convert it to a duration. Otherwise, you'll have to convert you time to one unit (decimal days, hours or minutes).
This page might help give you some ideas.
Here's a dummy example:
% creating a data set. Ignore this part
d=(datetime(2020,1,1):days(1):datetime(2020,1,20))';
t = duration({'09:00';'09:10';'09:00';'09:14';'09:59';'09:05';'09:00';'09:35';'13:15';'09:00'; ...
'09:00';'15:00';'09:35';'10:15';'14:10';'16:35';'09:00';'11:15';'09:00';'10:20'},'InputFormat','hh:mm');
% View as datetimes
D = d+t
D = 20×1 datetime array
01-Jan-2020 09:00:00 02-Jan-2020 09:10:00 03-Jan-2020 09:00:00 04-Jan-2020 09:14:00 05-Jan-2020 09:59:00 06-Jan-2020 09:05:00 07-Jan-2020 09:00:00 08-Jan-2020 09:35:00 09-Jan-2020 13:15:00 10-Jan-2020 09:00:00 11-Jan-2020 09:00:00 12-Jan-2020 15:00:00 13-Jan-2020 09:35:00 14-Jan-2020 10:15:00 15-Jan-2020 14:10:00 16-Jan-2020 16:35:00 17-Jan-2020 09:00:00 18-Jan-2020 11:15:00 19-Jan-2020 09:00:00 20-Jan-2020 10:20:00
% extract hour and minute using Hour and Minute properties, convert to duration
dur = duration(D.Hour, D.Minute,0,"Format",'hh:mm');
% Calculate the mean
mean(dur)
ans = duration
10:31
Eric Sofen
Eric Sofen 2020-12-18
A slight simplification. Instead of extracting the hours and minutes components, you can use the timeofday function to get durations that represent the time of day.

请先登录,再进行评论。

更多回答(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