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

2 ビュー (過去 30 日間)
Feral
Feral 2019 年 4 月 18 日
回答済み: Peter Perkins 2019 年 5 月 3 日
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'}]

採用された回答

Jan
Jan 2019 年 4 月 19 日
編集済み: Jan 2019 年 4 月 19 日
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 件のコメント
Jan
Jan 2019 年 4 月 23 日
@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.
Feral
Feral 2019 年 4 月 23 日
Yes! exaclty it. Thanks alot.

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2019 年 5 月 3 日
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by