필터 지우기
필터 지우기

Matching based on working days

조회 수: 4 (최근 30일)
Wesso
Wesso 2019년 4월 7일
댓글: Wesso 2019년 4월 9일
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?
  댓글 수: 3
Clay Swackhamer
Clay Swackhamer 2019년 4월 8일
What kind of data type is your date vector? If this is text stored in a cell we might want to consider converting to a datetime object. Otherwise if it is already a datetime object then you could use some built in features for those objects. You could find out in your code by running
whos A
in the command window.
Wesso
Wesso 2019년 4월 8일
My vector is serial matlab date numbers. I had excel dates and transferred them to matlab using x2mdate.
I tried
B=A-1;
C=A-4;
D=A-28
But some of the days were saturdays and sundays. I posted the question in order to produce them neatly. Otherwise,I will use a loop to say that if the date is Saturday use Friday date, if it is a Sunday use Monday...

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2019년 4월 8일
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
  댓글 수: 1
Rik
Rik 2019년 4월 8일
As a sidenote, the functions in this answer all support datetime inputs as well, despite the doc for weekday not mentioning this.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 4월 8일
If possible, switch to using datetime instead of serial date numbers. You can use the datetime options 'ConvertFrom', 'excel' or 'ConvertFrom', 'excel1904' to create the datetime array from the Microsoft Excel data.
If you use datetime you can use day (with the 'dayofweek' KIND input), isweekend, and/or dateshift.
D = datetime('today');
dayName = day(D, 'name')
dayCode = day(D, 'dayofweek')
isweekend(D) % As I write this it is Monday so this returns false
sundayBefore = dateshift(D, 'start', 'week')
isweekend(sundayBefore) % This returns true
If you want to go further with your classification of working days than just "weekday or weekend", the holidays function in Financial Toolbox accepts datetime as inputs.
  댓글 수: 1
Wesso
Wesso 2019년 4월 9일
thanks for the tip

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by