How to identify date numbers corresponding to non-

2019-09-03 12:05发布

问题:

I am working with time series data in MATLAB. I have got two vectors of date numbers, one of which relies on a somewhat subjective data source. If both vectors were perfectly accurate, all date numbers should correspond to trading days while one vector would be a 'proper subset' of the other. Unfortunately this is not the case because one of the vectors contains several date numbers which correspond to non-business days. I would like to find a way to replace all non-business days in this vector with the next available business day.

Example:

datenumbers = [736062;736063;736064;736065;736066;736067]
% corresponds to [wed, thu, fri, sat, sun, mon]

This contains [736065;736066] which corresponds to the upcoming weekend. Because these are not working days I would like to identify the date number corresponding to the subsequent monday and change both entries so that:

datenumbers = [736062;736063;736064;736067;736067;736067]
% corresponds to [wed, thu, fri, mon, mon, mon]

回答1:

One way to do it is simply to loop through all your values and change them if they are Saturday or Sunday.

datenumbers = [736062;736063;736064;736065;736066;736067];
for i = 1:length(datenumbers)
    weekDay = mod(datenumbers(i),7);
    if weekDay == 1 || weekDay == 2 
        datenumbers(i) = (3-weekDay) + datenumbers(i);
    end
end