Calculating mean for time series

2 visualizzazioni (ultimi 30 giorni)
BenL
BenL il 14 Gen 2017
Commentato: Star Strider il 18 Gen 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.

Risposte (1)

Star Strider
Star Strider il 14 Gen 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 Commenti
BenL
BenL il 18 Gen 2017
may i ask, what do you mean 'retain their correct relationships'? are you referring to the rows of data?
Star Strider
Star Strider il 18 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by