How to locate the index of a certain date for a date-time array?

53 ビュー (過去 30 日間)
Sevde Tavasli
Sevde Tavasli 2022 年 12 月 9 日
コメント済み: Sevde Tavasli 2022 年 12 月 10 日
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

採用された回答

Stephen23
Stephen23 2022 年 12 月 10 日
You are working with datetimes, so do not compare text!
dt = datetime(2017,1,1,(0:1:123).',0,0, 'Format','u MM dd eee HHmmss')
dt = 124×1 datetime array
2017 01 01 Sun 000000 2017 01 01 Sun 010000 2017 01 01 Sun 020000 2017 01 01 Sun 030000 2017 01 01 Sun 040000 2017 01 01 Sun 050000 2017 01 01 Sun 060000 2017 01 01 Sun 070000 2017 01 01 Sun 080000 2017 01 01 Sun 090000 2017 01 01 Sun 100000 2017 01 01 Sun 110000 2017 01 01 Sun 120000 2017 01 01 Sun 130000 2017 01 01 Sun 140000 2017 01 01 Sun 150000 2017 01 01 Sun 160000 2017 01 01 Sun 170000 2017 01 01 Sun 180000 2017 01 01 Sun 190000 2017 01 01 Sun 200000 2017 01 01 Sun 210000 2017 01 01 Sun 220000 2017 01 01 Sun 230000 2017 01 02 Mon 000000 2017 01 02 Mon 010000 2017 01 02 Mon 020000 2017 01 02 Mon 030000 2017 01 02 Mon 040000 2017 01 02 Mon 050000
ix = timeofday(dt)==0 & weekday(dt)==2
ix = 124×1 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
md = dt(ix)
md = datetime
2017 01 02 Mon 000000
  2 件のコメント
Stephen23
Stephen23 2022 年 12 月 10 日
"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 would have answerd that, but your explanation is incomplete. It sounds as if you want to split your data into lots of separate arrays, but doing so is
  • not a good use of MATLAB,
  • means that you cannot use the inbuilt mehods for grouping and processing groups of data:
Instead of asking us about how to split data into lots of arrays, you should actaully ask us about your actual goal or task with that data:
Sevde Tavasli
Sevde Tavasli 2022 年 12 月 10 日
Thank you for your help, I thought I knew what I needed to do to analyse my data but turns out I did not.
The method you stated above worked to solve what I was trying to do in the first part of my question, but you are right, from reading the attached help links what I really want to do is perform calcualtions by using groups in my table.
The links you attached were very helpful and have meant that I have been able to look at the data and perform calculations based on the day of the week as well as weeks!

サインインしてコメントする。

その他の回答 (1 件)

Arif Hoq
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);

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by