# Generate an 8-day datetime array between 2010 and 2021 which resets the 8-day count at the beginning of each year

1 ビュー (過去 30 日間)
Ziad Sari El Dine 2022 年 7 月 5 日
コメント済み: Ziad Sari El Dine 2022 年 7 月 5 日
Hello, I need to generate an 8-day datetime array between 2010 and 2021; however, i need the 8-day interval to reset at the beginning of each year. For example, the final date in 2010 would be December 27 and i would like the next date to be January 1 2011, not January 4. The code below gives the 8-day intervals without resetting.
t1 = datetime(2010,1,1,0,0,0);
t2 = datetime(2021,12,31,0,0,0);
t = t1:caldays(8):t2;

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

### 採用された回答

Steven Lord 2022 年 7 月 5 日
Jan1st = datetime(2010:2021, 1, 1);
We want to compute a vector of days for each January 1st that increases by 8 days until the end of the year. Luckily the number of days in the year is not a multiple of 8, so each year will have the same number of values represented.
dayIncrements = days(0:8:365).';
If you were using release R2020b or later you could use implicit expansion to sum Jan1st and dayIncrements to generate a matrix. If you need this as a vector you could reshape it. Since you indicated you're using release R2018a you'd need to repmat the two vectors.
t = Jan1st + dayIncrements;
t2 = repmat(Jan1st, numel(dayIncrements), 1) + repmat(dayIncrements, 1, numel(Jan1st));
check = isequal(t, t2)
check = logical
1
Now let's look at the first and last days in each column. Leap years end on December 26th, non leap years on December 27th.
firstAndLast = t([1 end], :)
firstAndLast = 2×12 datetime array
01-Jan-2010 01-Jan-2011 01-Jan-2012 01-Jan-2013 01-Jan-2014 01-Jan-2015 01-Jan-2016 01-Jan-2017 01-Jan-2018 01-Jan-2019 01-Jan-2020 01-Jan-2021 27-Dec-2010 27-Dec-2011 26-Dec-2012 27-Dec-2013 27-Dec-2014 27-Dec-2015 26-Dec-2016 27-Dec-2017 27-Dec-2018 27-Dec-2019 26-Dec-2020 27-Dec-2021
##### 1 件のコメント表示非表示 なし
Ziad Sari El Dine 2022 年 7 月 5 日
Thanks!

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

### カテゴリ

Find more on Calendar in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by