Creating index from datetime vector days

3 ビュー (過去 30 日間)
dan berkowitz
dan berkowitz 2017 年 11 月 17 日
回答済み: Peter Perkins 2017 年 11 月 20 日
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
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 件のコメント
dan berkowitz
dan berkowitz 2017 年 11 月 19 日
Works like a charm! Thank you! DB

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
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.

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by