0
votes

I have a matrix with a timestamp and several column variables. The matrix spans a month of half hourly variables. Here is a sample of four columns of the matrix

11/11/2015 20:15    31.26410236 35.70104634 35.93171056
11/11/2015 20:45    32.10746291 35.48806277 35.9647747
.
.
.
12/11/2015 20:15    32.10746291 35.48806277 35.9647747
12/11/2015 20:45    32.10746291 35.48806277 35.9647747
.
.
.
13/11/2015 20:15    32.68310429 35.58753807 37.26447422
13/11/2015 20:45    33.05141516 34.8432801  36.48033884
.
.
.
14/11/2015 20:15    32.08328579 34.66482668 34.65446868
14/11/2015 20:45    32.19994433 34.40562145 34.34035989

What is the easiest way to find the average of identical times in terms of hours and minutes? E.g. mean of each variable at time 20:45 for all days of the month.

I know I could achieve this by converting the timestamp to a datenum, taking the fractional part of datenum and sorting the data by the fractional part of datenum. After that I could block average the rows with similar fractional datenums. Is there a more efficient and more elegant way?

2

2 Answers

0
votes

With matlab you can work directly with date and times without converting it to timestamp in miliseconds or seconds:

http://es.mathworks.com/help/matlab/date-and-time-operations-1.html

Or an easy way is to convert dates to a date vector like this:

DateVector = datevec(DateString,formatIn)

then compare the columns you want:

[Y,M,D,H,MN,S] = datevec(___)

>> A = datevec('13/11/2015 20:45','dd/mm/yyyy HH:MM')

A =

        2015          11          13          20          45           0

>> B = datevec('14/11/2015 20:45','dd/mm/yyyy HH:MM')

B =

        2015          11          14          20          45           0

with this is easy to compare dates:

>> A - B

ans =

     0     0    -1     0     0     0

exactly one day difference

0
votes

This is what I ened up doing to solve this problem:

timestamp=linspace(datenum('2015-11-01 00:00', 'yyyy-mm-dd HH:MM'),datenum('2015-12-01 00:00', 'yyyy-mm-dd HH:MM'),1440); % 30 days
timestamp=timestamp';
time_of_day=datetime(datevec(timestamp(1:48)),'Format','HH:mm');
numdays=30;
data=rand(length(timestamp),2);
means=NaN(48,3);
for tt=1:48
    means(tt,:)=[datenum(time_of_day(tt)) nanmean(data(tt:48:48*numdays,:),1)];
end

    figure;
    plot(time_of_day,means(:,2:3));
    xlim([timestamp(1) timestamp(48)]);