필터 지우기
필터 지우기

Removing close datetimes from datetime array

조회 수: 10 (최근 30일)
Dylan den Hartog
Dylan den Hartog 2020년 5월 14일
댓글: Campion Loong 2020년 5월 22일
I have this 5x1 datetime array:
14-May-2020 10:47:18
14-May-2020 10:47:19
14-May-2020 10:47:20
14-May-2020 10:53:36
14-May-2020 10:54:05
I want to remove all datetimes within 2 seconds of another datetime, so in this case I want to remove the second and third datetime '14-May-2020 10:47:19' and '14-May-2020 10:47:20' and keep '14-May-2020 10:47:18'. Is there an easy way to do this?
  댓글 수: 2
Sindar
Sindar 2020년 5월 15일
diff will allow you determine the difference between times, but you haven't fully specified the problem, and some versions are significantly easier. Let's consider this example:
14-May-2020 10:47:18
14-May-2020 10:47:19
14-May-2020 10:47:20
14-May-2020 10:47:21
14-May-2020 10:53:36
14-May-2020 10:54:05
What remains based on various rules:
"remove all datetimes within 2 seconds of another datetime"
14-May-2020 10:53:36
14-May-2020 10:54:05
"remove all datetimes within 2 seconds of the previous datetime"
14-May-2020 10:47:18
14-May-2020 10:53:36
14-May-2020 10:54:05
"remove datetimes -- starting from the beginning -- so that none are within 2 seconds of each other"
14-May-2020 10:47:18
14-May-2020 10:47:21
14-May-2020 10:53:36
14-May-2020 10:54:05
Which one would you like?
Campion Loong
Campion Loong 2020년 5월 22일
If I may take a guess that Sindar's second example (i.e. "remove all datetimes within 2 seconds of the previous datetime") is the desired outcome, I'd do:
>> dt = sort(dt);
>> dt([true; diff(dt) >= seconds(2)]) % the leading TRUE accounts for the first element offset

댓글을 달려면 로그인하십시오.

답변 (1개)

Jalaj Gambhir
Jalaj Gambhir 2020년 5월 18일
편집: Jalaj Gambhir 2020년 5월 18일
Hi,
The following is one of the way in which you can achieve your task, as pointed out in the comment, "remove all datetimes within 2 seconds of the previous datetime"
DateStrings = {'14-May-2020 10:47:18','14-May-2020 10:47:19','14-May-2020 10:47:20','14-May-2020 10:53:36','14-May-2020 10:54:05', '14-May-2020 10:51:03', '14-May-2020 10:51:05'};
t = datetime(DateStrings,'InputFormat','dd-MMM-yyyy HH:mm:ss');
t = sort(t);
for i=1:length(t)
if ~isnat(t(i))
lower = t(i)+seconds(1);
upper = lower + seconds(2);
t(isbetween(t,lower,upper)) = NaT;
end
end
result = t(~isnat(t));
Hope this helps!

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by