Calculating mean for time series
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposte (1)
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
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.
Vedere anche
Categorie
Scopri di più su Time Series Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!