Creating index from datetime vector days
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a 100x1 datetime vector. I have extracted the values of the day using the ymd function: [~,~,d] = ymd(input).
How can I create a vector with a unique identifier for every period from the 15th to the 14th in the days vector, d (each sequence of 15 to the next 14). (i.e. from jan 15 - feb 14, i could have 1's, from feb 15 - mar 14, i can have 2s, etc.)
Any help would be appreciated. Thx in advance!
DB
0 个评论
采纳的回答
Walter Roberson
2017-11-17
Take the month number output as well as the day output. If the day is less than 15, subtract 1 from the month number to get the index (if the month number was 1, substitute 12 for previous year); otherwise use the month number directly.
更多回答(1 个)
Peter Perkins
2017-11-20
discretize does this for you:
>> dt = datetime(2017,1,1:10:100)'
dt =
10×1 datetime array
01-Jan-2017
11-Jan-2017
21-Jan-2017
31-Jan-2017
10-Feb-2017
20-Feb-2017
02-Mar-2017
12-Mar-2017
22-Mar-2017
01-Apr-2017
>> edges = datetime(2016,12,15) + calmonths(0:5)
edges =
1×6 datetime array
15-Dec-2016 15-Jan-2017 15-Feb-2017 15-Mar-2017 15-Apr-2017 15-May-2017
>> discretize(dt,edges,'categorical')
ans =
10×1 categorical array
[15-Dec-2016, 15-Jan-2017)
[15-Dec-2016, 15-Jan-2017)
[15-Jan-2017, 15-Feb-2017)
[15-Jan-2017, 15-Feb-2017)
[15-Jan-2017, 15-Feb-2017)
[15-Feb-2017, 15-Mar-2017)
[15-Feb-2017, 15-Mar-2017)
[15-Feb-2017, 15-Mar-2017)
[15-Mar-2017, 15-Apr-2017)
[15-Mar-2017, 15-Apr-2017)
If you really need numbers for the bins, just leave off the 'categorical' flag.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!