Creating index from datetime vector days

2 views (last 30 days)
dan berkowitz on 17 Nov 2017
Answered: Peter Perkins on 20 Nov 2017
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

Walter Roberson on 17 Nov 2017
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.
dan berkowitz on 19 Nov 2017
Works like a charm! Thank you! DB

Peter Perkins on 20 Nov 2017
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.