Conversion from daily to monthly frequency
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I have a time series - daily frequency - stored in a vector X. In addition, I have a vector T containing the corresponding date strings of this time series.
Based on X, I would like to generate a time series Z with monthly frequency. For instance, I would like to generate Z using the values asscociated with the last calendar day of each month.
Your help is very much appreciated!
kanimbla
댓글 수: 0
답변 (2개)
dpb
2014년 6월 8일
First idea -- create the datenum() vector of the time series and then the EOM (end-of-month) days for the year(s) included within. Then return those matching.
To create the EOM day vector
dn=datenum(yourtsdates); % full array datenum vector
[y,~]=datevec([dn(1) dn(end)]).'; % the years in the series
y=unique(y); % and eliminate duplicate if only one year
yy=repmat(y,1,12).'; yy=yy(:); % each year for each 12 months/year
dneom=datenum(yy,repmat([1:12].',length(y),1), ...
cell2mat(arrayfun(@(yr) eomday(yr, [1:12].'),y, ...
'uniformoutput',false)));
The last looks more complicated than it really is... :)
It computes the datenum for the eom days for each month of the years found in the timeseries (y). To match up the array sizes, it duplicates the months for that many years and then calls eomday for each year to build the array of days to go along with the year and month. This will automagically handle leap years, etc., ...
Then just find these positions in the overall date number vector.
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!