Intersect two datetime vectors with 1min tolerance
15 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Seth Furman
2022-2-24
编辑:Seth Furman
2022-2-25
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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!