Intersect two datetime vectors with 1min tolerance

6 views (last 30 days)
Hello everyone, I am currently trying to find the common values between two datetime vectors (attached) and return their indexes on their respective lists with a tolerance of +-1min. I was trying to approach this using intersect as so:
[COMPS_data_CGGPBR,i_CGGPBR,i_PBR] = intersect(data_tracking_CGGPBR,data_tracking_PBR, 'Rows');
However, I don't know how to add the 1 minute tolerance that I need. It is a shame, because this function gives exactly what I need: the common datetime and the indexes that they first appear on each list.
Since my thinking is apparently leading nowhere, what can I use to solve this problem? I have tried simple loops, but it would take me ~250 days to calculate it all.
Thanks in advance,
Arthur.

Answers (1)

Seth Furman
Seth Furman on 24 Feb 2022
Edited: Seth Furman on 25 Feb 2022
load(websave('data_tracking_PBR.mat', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/904455/data_tracking_PBR.mat'));
load(websave('data_tracking_CGGPBR.mat', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/904460/data_tracking_CGGPBR.mat'));
A = unique(data_tracking_PBR, "sorted");
B = unique(data_tracking_CGGPBR, "sorted");
tol = minutes(1);
i = 1;
j = 1;
ia = false(1, length(A));
ib = false(1, length(B));
while i <= length(A) && j <= length(B)
if abs(B(j)-A(i)) <= tol
ia(i) = true;
ib(j) = true;
if B(j) <= A(i)
j = j + 1;
else
i = i + 1;
end
elseif A(i) < B(j)-tol
i = i + 1;
else
j = j + 1;
end
end
C = union(A(ia), B(ib))
C = 205115×1 datetime array
31-May-2021 12:59:00 31-May-2021 13:00:00 31-May-2021 13:01:00 31-May-2021 13:02:00 31-May-2021 13:03:00 31-May-2021 13:04:00 31-May-2021 13:05:00 31-May-2021 13:06:00 31-May-2021 13:07:00 31-May-2021 13:08:00 31-May-2021 13:09:00 31-May-2021 13:10:00 31-May-2021 13:11:00 31-May-2021 13:12:00 31-May-2021 13:13:00 31-May-2021 13:14:00 31-May-2021 13:15:00 31-May-2021 13:16:00 31-May-2021 13:17:00 31-May-2021 13:18:00 31-May-2021 13:19:00 31-May-2021 13:20:00 31-May-2021 13:21:00 31-May-2021 13:22:00 31-May-2021 13:23:00 31-May-2021 13:24:00 31-May-2021 13:25:00 31-May-2021 13:26:00 31-May-2021 13:27:00 31-May-2021 13:28:00

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!