How to locate the index of a certain date for a date-time array?
이전 댓글 표시
Hello,
I have a date-time array, 'dt' that is recorded hourly for a whole year.
The first three data points are as follows, 01-Jan-2017 00:00:00, 01-Jan-2017 01:00:00, 01-Jan-2017 02:00:00.
I would like to find the index of every Monday(at 00:00:00) present in the data set . Below is what I have used, this is not feasable as I would need to know the exact date of every Monday, type it out row by row and save it to my matrix m.
m=zeros(52,1)
m(1)=find(datetime=='02-Oct-2017 00:00:00')
m(2)=find(datetime=='09-Oct-2017 00:00:00')
...
m(52)=find(datetime=='25-Dec-2017 00:00:00')
Is there a better way to do this?
What I am trying to achieve with this is to create new arrays that start on a Monday and last for a whole week, and do this for the span of a whole year.
I tried to use
[week,edges]=discretize(dt,"week")
but it does not start on a Monday, so I was not able to get it to work.
Thank you
채택된 답변
추가 답변 (1개)
Arif Hoq
2022년 12월 10일
I don't have your data. try this one:
% creating datetime data from 1st January 2017 to 31 December 2017
firstdate = '01-01-2017 00:00:00';
t1 = datetime(firstdate,'InputFormat','dd-MM-yyyy HH:mm:ss');
lastdate='31-12-2017 23:00:00';
t2 = datetime(lastdate,'InputFormat','dd-MM-yyyy HH:mm:ss');
oneyeardata=t1:+hours(1):t2;
realdate=oneyeardata(:);
dayname=day(realdate,'name');
strname=string(dayname);
dateday=table(realdate,strname);
findmonday=find(strcmp(string(table2cell(dateday(:,2))),"Monday"));
idxmonday=dateday(findmonday,1);
aa=string(table2cell(idxmonday));
bb=split(aa," ");
cc=find(strcmp(bb(:,2),'00:00:00'));
Monday_Only=idxmonday(cc,1);
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!