How can I add a column to a table where I look up the date against a list of holiday dates and if it is a holiday I create a new binary variable?

5 次查看(过去 30 天)
I have a file with data indexed by dates. The first column in the table in dates. I have another table of a list of US bank holidays. I want to look up the dates in my data file and create a new binary variable in the data table such that if it is a holiday I mark the variable 1 and if it is not a holiday I mark it 0.

回答(1 个)

Sonam Gupta
Sonam Gupta 2017-10-26
Hi,
Based on the description of your question, I have created a demo for you.
%dates in November month
t1 = datetime(2017, 10,1, 8, 0, 0);
t2 = datetime(2017, 10, 30, 8, 0, 0);
t = t1:t2;
t = t';
%serial number
a = 1:30;
a = a';
%generating table 1 with list of dates
mytable = table(a, t);
%generating holiday tables
hol = t1:caldays(5):t2;
holiday_table = table(hol');
%finding common dates between the two tables
d1 = datetime(mytable{:,2});
d2 = datetime(holiday_table{:,1});
common_dates = intersect (d1, d2);
rows = ismember(mytable.t, common_dates);
%concatenate it with the existing table
mytable = [mytable table(rows)];
I hope this helps.

类别

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