Calculating mean for time series

4 views (last 30 days)
BenL
BenL on 14 Jan 2017
Commented: Star Strider on 18 Jan 2017
In matrix A, I have two columns:
Col 1 = Time (in Excel serial date format e.g. 4.264400001156250e+04 = 01-Oct-2016 00:00:00) Col 2 = Measurement
The time are in intervals of few seconds, and matrix A has about 400,000 rows, i.e. A(400000,2).
My question is, how can I script to compute the average of the Measurement variable for each day? I can do this in Excel, but it seems awfully time consuming.
Thank you.

Answers (1)

Star Strider
Star Strider on 14 Jan 2017
I would begin by converting all the dates to integer date numbers:
de = datetime(4.264400001156250e+04, 'ConvertFrom','excel'); % Convert From Excel Date
dn = datenum(de); % Date Number (Includes Fractional Days)
day_only = fix(dn); % Date Number (Integer Days Only)
After that, I would use the unique function with the third output, and use it as the index variable for the accumarray call, something like this:
[Du,~,di] = unique(Data, 'stable');
Means = accumarray(di, Data, [], @mean);
Out = [datevec(Du) Means]
Without your data, this is only a ‘sketch’ of sorts. It would have to be modified to work with your actual data.
  6 Comments
BenL
BenL on 18 Jan 2017
may i ask, what do you mean 'retain their correct relationships'? are you referring to the rows of data?
Star Strider
Star Strider on 18 Jan 2017
Yes. The 'stable' argument keeps the dates and times (and all references to them) in the original order in the vector, rather than sorting them, which is the default behavior. This creates a ‘di’ vector that will correctly locate the date indices in it with the data associated with the dates and times. The output of the accumarray call will then be correct.
See the documentation for the unique function for details.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!