Grouping based upon differences in time, with some small scatter

3 次查看(过去 30 天)
Hello!
We have an application to analyze digital signals from several events on separate instruments. There is a time stamp in each record. There can be a small offset in the time recorded (<30 seconds) for a single event on separate instruments. For example:
{[27-Aug-2020 12:30:13]}
{[27-Aug-2020 12:39:23]}
{[27-Aug-2020 12:47:46]}
{[27-Aug-2020 12:30:15]}
{[27-Aug-2020 12:39:25]}
{[27-Aug-2020 12:47:48]}
{[27-Aug-2020 12:30:18]}
{[27-Aug-2020 12:39:28]}
So there are three events (~12:30, ~12:39, ~12:47), the first two recorded on three instruments, the last event on only two. Since the delay may be up to 30 seconds, ignoring the seconds isn't an option.
In practice there may be tens of events and a hundred instruments, so grouping this is a bit of a pain. I can use datenum() and then seconds() to get whether the difference is less than 30 seconds, but kind of hung up on what to do next.
Any suggestions?
Thanks!
Doug Anderson

采纳的回答

Duncan Po
Duncan Po 2021-2-18
One way to do this is to use isbetween in a for loop, and then unique(, 'rows') to find the groups:
t = datetime({'27-Aug-2020 12:30:13',
'27-Aug-2020 12:39:23',
'27-Aug-2020 12:47:46',
'27-Aug-2020 12:30:15',
'27-Aug-2020 12:39:25',
'27-Aug-2020 12:47:48',
'27-Aug-2020 12:30:18',
'27-Aug-2020 12:39:28'}); % put the data in a datetime
tmax = t + seconds(30); % determine the 30s offsets from each element
tmin = t - seconds(30);
for i = 1:length(t) % loop through each element
y(i,:) = isbetween(t, tmin(i), tmax(i));
end
yy = unique(y, 'rows') % each row is a logical vector indicating which element belongs to the same group

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by