MATLAB: Calculating the average value of some variables during each sample time

matrixmatrix array

Hi, I have data that contains one columns for time and some columns for values of some variables the same as following (this is a part of my data):
A=[ 29/07/2014 08:00:00.000 152 28 2;
29/07/2014 08:00:01.000 100 20 8;
29/07/2014 08:00:02.000 201 45 7;
29/07/2014 08:00:03.000 149 23 6]
First, the start time, finish time and sample time (time interval) are given by user. I want to calculate the average value for each variable during each sample time that user specified. Suppose that user has given the following information: Start time: ‘29/07/2014 08:00:00.000’; Finish time: ‘29/07/2014 08:20:00.000’; Sample time: 5 ;% based on minute. I Finally want to have a set of data as the following:
F=[29/07/2014 08:00:00.000X1 Y1 Z1;
29/07/2014 08:05:00.000X2 Y2 Z2;
29/07/2014 08:10:00.000X3 Y3 Z3;
29/07/2014 08:15:00.000X4 Y4 Z4;
29/07/2014 08:20:00.000X5 Y5 Z5];
That for example X2 is the average of X values between ‘29/07/2014 08:00:00.000’ and ‘29/07/2014 08:05:00.000’ and X3 is the average of X values between ‘29/07/2014 08:05:00.000’ and ‘29/07/2014 08:10:00.000’. Would you please help me how can I do this?

Best Answer

A={ '29/07/2014 08:00:00.000' 152 28 2;
'29/07/2014 08:08:01.000' 100 20 8;
'29/07/2014 08:12:02.000' 201 45 7;
'29/07/2014 08:13:03.000' 149 23 6
'29/07/2014 08:15:03.000' 149 23 6
'29/07/2014 08:16:03.000' 149 23 6
'29/07/2014 08:18:03.000' 149 23 6}
t2 = datenum(2014,7,29,8,(0:5:20)',0);
t1 = datenum(A(:,1),'dd/mm/yyyy HH:MM:SS.FFF');
[~,ii] = histc(t1,[t2;inf]);
B = cell2mat(A(:,2:end));
s = size(B); % EDIT
[r,c] = ndgrid(ii,1:s(2));
F = [datevec(t2),accumarray([r(:),c(:)],B(:),[numel(t2),s(2)],@mean)];