且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何识别与非工作日对应的日期数字,并在MATLAB中的下一个可用工作日更换它们

更新时间:2022-12-08 15:35:49

一种方法是循环使用所有的值,如果是星期六或星期日,则更改它们。 >

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


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]

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