Extract rows in one array based on time values in a another array +20 seconds

1 次查看(过去 30 天)
I have two arrays. Array A is a 29 x 1 vector that includes different times that an event occurred. Array B is a 1410 x 4 matrix where the first column contains time values as well (some times are the same as those in matrix A and some are different).
Truncated example of array A:
399.760000000000
400.140000000000
400.390000000000
400.760000000000
Truncated example of array B:
399.010000000000 2 1 NaN
399.760000000000 16 NaN NaN
400.140000000000 16 NaN NaN
400.390000000000 16 NaN NaN
400.760000000000 16 NaN NaN
401.630000000000 NaN NaN 45.4277500000000
405.080000000000 NaN NaN 45.3810000000000
413.320000000000 NaN NaN 42.3262500000000
415.950000000000 NaN NaN 43.1500000000000
420.200000000000 NaN NaN 38.6815000000000
422.750000000000 NaN NaN 38.0767500000000
429.250000000000 NaN NaN 44.4442500000000
What I want to do is extract all the rows in array B that are equal to the times in array A but also any rows in array B that are within a +20 second time window of the times in array A. So, from the above example I would want this:
Array C =
399.760000000000 16 NaN NaN
400.140000000000 16 NaN NaN
400.390000000000 16 NaN NaN
400.760000000000 16 NaN NaN
401.630000000000 NaN NaN 45.4277500000000
405.080000000000 NaN NaN 45.3810000000000
413.320000000000 NaN NaN 42.3262500000000
415.950000000000 NaN NaN 43.1500000000000
420.200000000000 NaN NaN 38.6815000000000
I have tried converting both A and B to times tables and using tolerances but I can't seem to get it to work correctly. I'm thinking using some kind of tolerance code may be the way to go but I have tried it many different ways and can't get it to work the way I want. Plus, tolerances want to look both +/-20 seconds when I really only need to look +20 seconds forward in time. Any help is much appreciated. I'm happy to make any clarifications if need be. Thanks.

采纳的回答

Akira Agata
Akira Agata 2019-4-23
Simple and straight-forward way would be like this.
Just in case, I have saved your truncated example of array A and B as .mat file and attached here.
idx = false(size(B,1),1);
for kk = 1:numel(A)
idx = idx | (B(:,1) >= A(kk) & B(:,1) <= A(kk) + 20);
end
C = B(idx,:);

更多回答(1 个)

Peter Perkins
Peter Perkins 2019-5-3
I'm not 100% following, but I would think a timetable would be your friend here. The following selects rows of a timetable that are at or no more than 20s after two specified times:
>> tt = timetable((1:10)','RowTimes',seconds(sort(100*rand(10,1))))
tt =
10×1 timetable
Time Var1
__________ ____
18.687 sec 1
27.603 sec 2
38.156 sec 3
44.559 sec 4
48.976 sec 5
64.631 sec 6
70.936 sec 7
75.469 sec 8
76.552 sec 9
79.52 sec 10
>> timesOfInterest = seconds(sort(100*rand(2,1)))
timesOfInterest =
2×1 duration array
34.039 sec
58.527 sec
>> tt(withtol(timesOfInterest+seconds(10),seconds(10)),:)
ans =
7×1 timetable
Time Var1
__________ ____
38.156 sec 3
44.559 sec 4
48.976 sec 5
64.631 sec 6
70.936 sec 7
75.469 sec 8
76.552 sec 9

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by