See if datetime is within range

조회 수: 3 (최근 30일)
Joy
Joy 2024년 3월 25일
댓글: Joy 2024년 3월 25일
I have two datasets of datetime and numerical values where the data was collected at incongruous times. For example:
Dataset 1:
'3/25/2024 15:01:15' 1
'3/25/2024 15:01:31' 2
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
'3/25/2024 15:02:51' 1
'3/25/2024 15:03:07' 2
Dataset 2:
'3/25/2024 15:01:10' 10
'3/25/2024 15:01:26' 20
'3/25/2024 15:01:42' 30
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
'3/25/2024 15:02:46' 70
I'd like to delete rows from dataset 1 and 2 that are in the same range of datetime. In this example, I'd like to delete the rows from dataset 1:
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
and therefore delete rows with the datetime in the same range from dataset 2:
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
  댓글 수: 2
Voss
Voss 2024년 3월 25일
You want to delete rows 3, 4, 5, and 6 from dataset 1, and also delete any rows from dataset 2 where the datetimes are within the range of the rows deleted from dataset 1. Is that right?
If so, why would rows 2 and 3 be deleted from dataset 2? Those are not in range (they are both before the first deleted dataset 1 datetime, '3/25/2024 15:01:47'). Only rows 4, 5, and 6 in dataset 2 are within the range '3/25/2024 15:01:47' to '3/25/2024 15:02:35'.
Joy
Joy 2024년 3월 25일
Pardon me, you're correct. It has been corrected.

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

채택된 답변

Star Strider
Star Strider 2024년 3월 25일
I do not entirely understand what you want to do.
One approach —
D1 = {'3/25/2024 15:01:15' 1
'3/25/2024 15:01:31' 2
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
'3/25/2024 15:02:51' 1
'3/25/2024 15:03:07' 2};
D2 = {'3/25/2024 15:01:10' 10
'3/25/2024 15:01:26' 20
'3/25/2024 15:01:42' 30
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
'3/25/2024 15:02:46' 70};
T1 = cell2table(D1);
T1.D11 = datetime(T1.D11)
T1 = 8x2 table
D11 D12 ____________________ ___ 25-Mar-2024 15:01:15 1 25-Mar-2024 15:01:31 2 25-Mar-2024 15:01:47 0 25-Mar-2024 15:02:03 0 25-Mar-2024 15:02:19 0 25-Mar-2024 15:02:35 0 25-Mar-2024 15:02:51 1 25-Mar-2024 15:03:07 2
T2 = cell2table(D2);
T2.D21 = datetime(T2.D21)
T2 = 7x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:10 10 25-Mar-2024 15:01:26 20 25-Mar-2024 15:01:42 30 25-Mar-2024 15:01:58 40 25-Mar-2024 15:02:14 50 25-Mar-2024 15:02:30 60 25-Mar-2024 15:02:46 70
Lv = T1.D12 == 0;
T1Q = T1.D11(Lv);
T2Q = T2.D21 >= T1Q(1) & T2.D21 <= T1Q(end); % Logical Vector
T2deleted = T2(T2Q,:) % Deleted Rows
T2deleted = 3x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:58 40 25-Mar-2024 15:02:14 50 25-Mar-2024 15:02:30 60
T2new = T2(~T2Q,:) % Retained Rows
T2new = 4x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:10 10 25-Mar-2024 15:01:26 20 25-Mar-2024 15:01:42 30 25-Mar-2024 15:02:46 70
.

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by