Calculating mean for time series

조회 수: 4 (최근 30일)
BenL
BenL 2017년 1월 14일
댓글: Star Strider 2017년 1월 18일
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.

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by