change timetable variable (working day to holiday)
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi all,
i have a timetable that specifies whether a date listed is working day or not-working day, by assigning 1 for working day and 0 for not-working day.
I wish to implement the holidays to the variable 'working day' of the timetable as well by changing the working day (1) to not-working day (0) for the specified dates in the variable 'holiday'.
holiday = {.... '2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'}
How may i implement this change?
Thank you.
댓글 수: 0
채택된 답변
Star Strider
2020년 9월 26일
Try this:
dd = datetime(2016,01,01):days(1):datetime(2016,12,31); % Create ‘datetime’ Array
workday = ones(size(dd(:))); % Define Original ‘workday’
holiday = datetime({'2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'});
TT1 = timetable(dd(:), workday); % Create ‘timetable’ Array
TF = ~ismember(TT1.Time,holiday); % Compare ‘Time’ & ‘holiday’
TT1.workday = +TF; % The ‘+’ Converts ‘TF’ To Numeric
with:
First_5_Rows = TT1(1:5,:)
producinmg:
First_5_Rows =
5×1 timetable
Time workday
___________ _______
01-Jan-2016 0
02-Jan-2016 1
03-Jan-2016 1
04-Jan-2016 1
05-Jan-2016 1
.
댓글 수: 2
Star Strider
2020년 9월 26일
My pleasure!
Sure! Add these assignments:
wkdaynr = weekday(TT1.Time); % Numeric Values For ‘weekday’
wdays = ~((wkdaynr == 1) | (wkdaynr == 7)); % Eliminate Saturday & Sunday
so the full code is now:
dd = datetime(2016,01,01):days(1):datetime(2016,12,31); % Create ‘datetime’ Array
workday = ones(size(dd(:))); % Define Original ‘workday’
TT1 = timetable(dd(:), workday); % Create ‘timetable’ Array
holiday = datetime({'2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'});
hdays = ~ismember(TT1.Time,holiday); % Compare ‘Time’ & ‘holiday’
wkdaynr = weekday(TT1.Time); % Numeric Values For ‘weekday’
wdays = ~((wkdaynr == 1) | (wkdaynr == 7)); % Eliminate Saturday & Sunday
TT1.workday = +(hdays & wdays); % Logically ‘AND’ Together, The ‘+’ Converts ‘TF’ To Numeric
with:
First_15_Rows = TT1(1:15,:)
producing:
First_15_Rows =
15×1 timetable
Time workday
___________ _______
01-Jan-2016 0
02-Jan-2016 0
03-Jan-2016 0
04-Jan-2016 1
05-Jan-2016 1
06-Jan-2016 1
07-Jan-2016 1
08-Jan-2016 1
09-Jan-2016 0
10-Jan-2016 0
11-Jan-2016 1
12-Jan-2016 1
13-Jan-2016 1
14-Jan-2016 1
15-Jan-2016 1
The first 3 lines of my code create the timetable array you already have, so just use the last 5 lines (after the blank line). Change ‘TT1’ to your timetable name.
.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!