MATLAB: Matching based on working days

dates

Hi,
I have a vector A of dates and I want to create a vectors of working days that are 1 day prior, 1 week prior and 4 week prior to vector A dates.
How can I do so?

Best Answer

You can use the weekday function. You can easily adapt the function below to apply specific shifts.
A=datenum({'2019-04-07','2019-04-08','2019-04-06'});
B=A-1;
C=A-4;
D=A-28;
B=move_to_workday(B);
C=move_to_workday(C);
D=move_to_workday(D);
function A=move_to_workday(A)
%shift sat to fri, sun to mon, and leave the rest
% %option 1:
% isSunday=weekday(A)==1;
% isSaturday=weekday(A)==2;
% A(isSunday)=A(isSunday)+1;
% A(isSaturday)=A(isSaturday)-1;
%option 2:
shift=[1 0 0 0 0 0 -1];
%shift(weekday(A)) retains the shape of shift, instead of taking on the
%size of A, hence the reshape
A=A+reshape(shift(weekday(A)),size(A));
end