How to select a time from one array which is close to the time in another array ?
Show older comments
I have 2 time arrays (input)
A = [{'05:10:34'} {'05:42:52'} {'06:52:17'} {'07:34:36'} {'07:44:37'} {'08:44:35'}]
B = [{'05:07:46'} {'05:14:43'} {'05:18:25'} {'05:40:04'} {'05:46:02'} {'06:00:32'} {'06:29:36'} {'06:49:31'} {'07:31:48'} {'07:41:49'} {'07:47:48'}]
need to select time of B which is closest to time in A (but time difference between A and B should not be more than 15 min).
and the time in B that has least time difference from A should be the desired output as below:
c = [{'05:07:46'} {'05:40:04'} {'06:49:31'} {'07:31:48'} {'07:41:49'}]
Accepted Answer
More Answers (1)
Peter Perkins
on 3 May 2019
N loops needed for this. Use durations amd 'nearest' interpolation. Actually, this problem is probably ill-posed, because if these are times of day, they'd better be within the same day. If they are not, you are in troubhle. If they are, try this:
>> A = [{'05:10:34'} {'05:42:52'} {'06:52:17'} {'07:34:36'} {'07:44:37'} {'08:44:35'}];
>> B = [{'05:07:46'} {'05:14:43'} {'05:18:25'} {'05:40:04'} {'05:46:02'} {'06:00:32'} {'06:29:36'} {'06:49:31'} {'07:31:48'} {'07:41:49'} {'07:47:48'}];
>> A = duration(A)
A =
1×6 duration array
05:10:34 05:42:52 06:52:17 07:34:36 07:44:37 08:44:35
>> B = duration(B)
B =
1×11 duration array
05:07:46 05:14:43 05:18:25 05:40:04 05:46:02 06:00:32 06:29:36 06:49:31 07:31:48 07:41:49 07:47:48
>> B2 = [seconds(0) B seconds(86400)] % make extrapolation work
B2 =
1×13 duration array
Columns 1 through 11
0 sec 18466 sec 18883 sec 19105 sec 20404 sec 20762 sec 21632 sec 23376 sec 24571 sec 27108 sec 27709 sec
Columns 12 through 13
28068 sec 86400 sec
>> closestBIndex = interp1(B2,[1 1:length(B) length(B)],A,'nearest')
closestBIndex =
1 4 8 9 10 11
>> closestBTime = B(closestBIndex)
closestBTime =
1×6 duration array
05:07:46 05:40:04 06:49:31 07:31:48 07:41:49 07:47:48
>> diffToClosestBTime = closestBTime - A
diffToClosestBTime =
1×6 duration array
-00:02:48 -00:02:48 -00:02:46 -00:02:48 -00:02:48 -00:56:47
>> closestBIndexWithin15 = abs(diffToClosestBTime) < minutes(15)
closestBIndexWithin15 =
1×6 logical array
1 1 1 1 1 0
>> B(closestBIndexWithin15)
ans =
1×5 duration array
05:07:46 05:14:43 05:18:25 05:40:04 05:46:02
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!