create a sub date time array from a datetime array

5 vues (au cours des 30 derniers jours)
ismail bhamjee
ismail bhamjee le 16 Déc 2019
Modifié(e) : Adam Danz le 18 Déc 2019
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 commentaires
dpb
dpb le 16 Déc 2019
Modifié(e) : dpb le 17 Déc 2019
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 le 18 Déc 2019
Modifié(e) : Adam Danz le 18 Déc 2019
@ ismail bhamjee, how does this question differ from the one you asked a couple hours earlier (and contains an ignored answer)?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by