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

107 Views Asked by At

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')
1

There are 1 best solutions below

0
On

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)

So, first remove dates and then compute the average:

% generating some random datetimes
dtn = datenum('2000-01-01') + rand(100, 1) * ...
    (datenum('2020-12-31')-datenum('2000-01-01'));
dt = datetime(dtn, 'convertfrom', 'datenum');

% finding mean of times
t = timeofday(dt); % elapsed time since midnight
m = mean(t)
datestr(m, 'hh:MM')