create a sub date time array from a datetime array

조회 수: 9 (최근 30일)
ismail bhamjee
ismail bhamjee 2019년 12월 16일
편집: Adam Danz 2019년 12월 18일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by