Generate an 8-day datetime array between 2010 and 2021 which resets the 8-day count at the beginning of each year
2 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Steven Lord
2022-7-5
Let's start with a row vector of January 1sts.
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)
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], :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!