How to select a time from one array which is close to the time in another array ?

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

Prefer to use modern datetime objects instead of cellstrings containing datestr values:
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 = datetime(A);
b = datetime(B);
c = NaT(size(a));
for k = 1:numel(A)
[dist, index] = min(abs(a(k) - b));
if minutes(dist) < 15
c(k) = b(index);
end
end
If you really need the olde datestr elements:
a = datenum(A);
b = datenum(B);
c = cell(size(a));
for k = 1:numel(A)
[dist, index] = min(abs(a(k) - b));
if dist < 1 / 96 % Explicitly: 15*60/86400
c{k} = B{index};
end
end

5 Comments

Thanks ! the code is perfectly fine , but how to find the indices of A when the time is <15 min.
@Feral: The code does find the indices already, but does not store it. If you want to collect the indices also, simply add:
iA = nan(size(a));
for k = 1:numel(A)
[dist, index] = min(abs(a(k) - b));
if minutes(dist) < 15
c(k) = b(index);
iA(k) = index;
end
end
@Jan: I did the same before asking the previous question but the code gives me the index of B i.e [1,4,8,9,10,NaN] what I want is index of A which would be [1,2,3,4,5,NaN].
@Freal: Ah, I understand. Then replace:
iA(k) = index;
by
iA(k) = k;
It is not hard, isn't it? The loop runs over the elements of A, so the loop index is the wanted index relalted to A also.

Sign in to comment.

More Answers (1)

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

Tags

Asked:

on 18 Apr 2019

Answered:

on 3 May 2019

Community Treasure Hunt

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

Start Hunting!