create a sub date time array from a datetime array
이전 댓글 표시
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.

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
2019년 12월 16일
d(1981-01-01) parses to d(1979) because it looks like and is a valid arithmetic expression.
To process arrays 10 at a time often it is most effective to just reshape by the number wanted. Then one can operate on the resulting array by column with Matlab's vectorized functions that by default operate by column.
More detail of what you're really after here could help define more optimal operations than creating multiple arrays is likely to be.
ismail bhamjee
2019년 12월 16일
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>
@ ismail bhamjee, how does this question differ from the one you asked a couple hours earlier (and contains an ignored answer)?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!