intersect two datetime vectors
Mostrar comentarios más antiguos
Hi,
I have 2 datetime vectors of different size, I'm trying to find the indices of identical timestamps in both vectors. Intersect and Ismember return empty or zero respectively, Datestr doesnt work, I assume something is wrong with the datetime vectors (attached)?
cheers,
Respuesta aceptada
Más respuestas (3)
Akira Agata
el 13 de En. de 2019
Editada: Akira Agata
el 13 de En. de 2019
The following can find identical timestamp with 0.1 sec tolerance.
% Load data
load('example1.mat');
% Find identical timestamp with 0.1 sec tolerance
dt = 0.1; %[sec]
tol = (dt/(60*60*24))/max(datenum([timeC;timeU]));
[idx,loc] = ismembertol(datenum(timeC),datenum(timeU),tol);
% Extract identical timestamp
timeC2 = timeC(idx);
timeU2 = timeU(loc(idx));
The result is:
% Check the result
timeC2.Format = 'yyyy/MM/dd HH:mm:ss.SSS';
timeU2.Format = 'yyyy/MM/dd HH:mm:ss.SSS';
disp([timeC2,timeU2])
>> disp([timeC2,timeU2])
2018/11/22 16:20:30.000 2018/11/22 16:20:29.917
2018/11/22 18:50:04.000 2018/11/22 18:50:04.051
2018/11/22 20:05:07.000 2018/11/22 20:05:07.041
2018/11/22 21:11:25.000 2018/11/22 21:11:24.912
...
1 comentario
Walter Roberson
el 13 de En. de 2019
Note that hypothetically a time can be within 0.1 tolerance of two different samples that are not within 0.1 tolerance of each other.
Walter Roberson
el 10 de En. de 2019
There are no identical timestamps between the two.
timeC.Format = 'dd-MMM-uuuu HH:mm:ss.SSSSSSSSS';
timeU.Format = 'dd-MMM-uuuu HH:mm:ss.SSSSSSSSS';
>> timeC(83957),timeU(24308)
ans =
datetime
22-Nov-2018 22:27:40.000000000
ans =
datetime
22-Nov-2018 22:27:39.966000000
>> dddd = timeC(83957)-timeU(24308); dddd.Format = 'hh:mm:ss.SSSSSSSSS'
dddd =
duration
00:00:00.034000000
There is no smaller time difference (but there might be some other places with the same difference.)
Peter Perkins
el 23 de En. de 2019
0 votos
As others have said, you don't have any matches between those two datetime vectors. The seem to be off by anything from about .5s to about 1s.
Does 22-Nov-2018 15:42:28.482 match 22-Nov-2018 15:42:28.000 in the other? I guess so. But then does 22-Nov-2018 22:27:58.965 match 22-Nov-2018 22:27:58.000 in the other? or 22-Nov-2018 22:27:59.000.
I think your problem is very ill-posed, and just making the code run without erroring is not your problem. So, some advice:
1) Do not use datenums. If you must convert datetimes to numeric in order to use ismembertol, use posixtime or something similar.
2) withtol will give you want clearly seem like wrong answers.
3) If the issue is that you have a progressive drift, use dateshift to remove that fractional part.
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!