필터 지우기
필터 지우기

A (simple) way to use ismember between datetime arrays with different formats

조회 수: 10 (최근 30일)
Is there a (simple) way to use ismember between datetime arrays with different formats?
In the following example, I want to find the dates that are present both in A and in B, regardless the time (i.e. hours, minutes, seconds):
% Input
A = datetime(...
['19-Jun-2023',
'20-Jun-2023',
'21-Jun-2023',
'22-Jun-2023',
'23-Jun-2023',
'24-Jun-2023',
'25-Jun-2023']);
tmp = [{'23-Jun-2023 19:00:00'}
{'24-Jun-2023 16:00:00'}
{'24-Jun-2023 11:00:00'}
{'19-Jun-2023 16:00:00'}
{'20-Jun-2023 10:00:00'}
{'21-Jun-2023 10:00:00'}
{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 14:00:00'}
{'19-Jun-2023 17:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 17:00:00'}
{'22-Jun-2023 15:00:00'}
{'23-Jun-2023 06:00:00'}
{'24-Jun-2023 11:00:00'}
{'25-Jun-2023 19:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 09:00:00'}
{'23-Jun-2023 12:00:00'}
{'25-Jun-2023 17:00:00'}
{'23-Jun-2023 12:00:00'}
{'22-Jun-2023 07:00:00'}];
B = datetime(tmp, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss');
ismember(A,B)
ans = 7x1 logical array
0 0 0 0 0 0 0
  댓글 수: 2
Stephen23
Stephen23 2024년 6월 21일
편집: Stephen23 2024년 6월 21일

The format is completely irrelevant. What matters is the date and time. If you want to ignore the time-of-day then use DATESHIFT before calling ISMEMBER.

Sim
Sim 2024년 6월 21일
편집: Sim 2024년 6월 21일
Thanks a lot! I think I found that kind of solution at the same moment you wrote it :-)
I used:
dateshift(B, 'start', 'day');

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

채택된 답변

Benjamin Kraus
Benjamin Kraus 2024년 6월 21일
I think the function you are looking for is dateshift.
A = datetime(...
['19-Jun-2023',
'20-Jun-2023',
'21-Jun-2023',
'22-Jun-2023',
'23-Jun-2023',
'24-Jun-2023',
'25-Jun-2023']);
tmp = [{'23-Jun-2023 19:00:00'}
{'24-Jun-2023 16:00:00'}
{'24-Jun-2023 11:00:00'}
{'19-Jun-2023 16:00:00'}
{'20-Jun-2023 10:00:00'}
{'21-Jun-2023 10:00:00'}
{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 14:00:00'}
{'19-Jun-2023 17:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 17:00:00'}
{'22-Jun-2023 15:00:00'}
{'23-Jun-2023 06:00:00'}
{'24-Jun-2023 11:00:00'}
{'25-Jun-2023 19:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 09:00:00'}
{'23-Jun-2023 12:00:00'}
{'25-Jun-2023 17:00:00'}
{'23-Jun-2023 12:00:00'}
{'22-Jun-2023 07:00:00'}];
B = datetime(tmp, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss');
ismember(A,dateshift(B,'start','day'))
ans = 7x1 logical array
1 1 1 1 1 1 1

추가 답변 (1개)

Sim
Sim 2024년 6월 21일
Maybe I found a way, but I am not sure about "dateshift(B, 'start', 'day')":
% Input
A = datetime(...
['19-Jun-2023',
'20-Jun-2023',
'21-Jun-2023',
'22-Jun-2023',
'23-Jun-2023',
'24-Jun-2023',
'25-Jun-2023']);
tmp = [{'23-Jun-2023 19:00:00'}
{'24-Jun-2023 16:00:00'}
{'24-Jun-2023 11:00:00'}
{'19-Jun-2023 16:00:00'}
{'20-Jun-2023 10:00:00'}
{'21-Jun-2023 10:00:00'}
{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 14:00:00'}
{'19-Jun-2023 17:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 17:00:00'}
{'22-Jun-2023 15:00:00'}
{'23-Jun-2023 06:00:00'}
{'24-Jun-2023 11:00:00'}
{'25-Jun-2023 19:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 09:00:00'}
{'23-Jun-2023 12:00:00'}
{'25-Jun-2023 17:00:00'}
{'23-Jun-2023 12:00:00'}
{'22-Jun-2023 07:00:00'}];
% Solution
B = datetime(tmp, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss','Format', 'dd-MMM-yyyy');
B2 = dateshift(B, 'start', 'day');
ismember(B2,A)
ans = 21x1 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by