create a sub date time array from a datetime array

5 次查看(过去 30 天)
I have a date time array which has many dates ranging from 1981 till 2017. I would like to create smaller arrays of the years for example and array of 10 years at a time.
Capture.PNG
my data is kept in one column.
i've tried
>> t=d(1981-01-01):calyears(1):d(1999-12-31)
d being the dates and t being the new array i want to create.
i get and error of
Index in position 2 exceeds array bounds (must not exceed 1)
the first entry in the date time array is 01-12-1981
  4 个评论
dpb
dpb 2019-12-16
编辑:dpb 2019-12-17
Again, why create a new column? What is next after that? Why not just use findgroups and splitapply to process the desired subset(s)? Creating duplicates of data just to do so isn't efficient nor best use of Matlab.
We can't see what would be the benefit from the information so far...
[g gyr]=findgroups(dt.Year); % find grouping variable by year and years in list
Now splitapply or other logical addressing operations using g and matching will give you all you asked for and more without duplicating the data itself; just pick what is need for a given task.
>> dt=datetime({'01-dec-1981';'03-jan-1983';'23-may-1990';'17-jun-2012'});
>> [g,id]=findgroups(dt.Year)
g =
1
2
3
4
id =
1981
1983
1990
2012
>>
However, what you may be looking for is but not knowing it is the timetable, see <TIMETABLE> and particular the link to the section subscript-into-times-of-timetable>
Adam Danz
Adam Danz 2019-12-18
编辑:Adam Danz 2019-12-18
@ ismail bhamjee, how does this question differ from the one you asked a couple hours earlier (and contains an ignored answer)?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by