Conversion from daily to monthly frequency

2 views (last 30 days)
kanimbla
kanimbla on 8 Jun 2014
Commented: kanimbla on 8 Jun 2014
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

Answers (2)

Star Strider
Star Strider on 8 Jun 2014
The eomday function is just waiting for you to discover it!

dpb
dpb on 8 Jun 2014
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.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!