# Removing close datetimes from datetime array

8 ビュー (過去 30 日間)
Dylan den Hartog 2020 年 5 月 14 日
コメント済み: Campion Loong 2020 年 5 月 22 日
I have this 5x1 datetime array:
14-May-2020 10:47:18
14-May-2020 10:47:19
14-May-2020 10:47:20
14-May-2020 10:53:36
14-May-2020 10:54:05
I want to remove all datetimes within 2 seconds of another datetime, so in this case I want to remove the second and third datetime '14-May-2020 10:47:19' and '14-May-2020 10:47:20' and keep '14-May-2020 10:47:18'. Is there an easy way to do this?
##### 2 件のコメント表示非表示 1 件の古いコメント
Campion Loong 2020 年 5 月 22 日
If I may take a guess that Sindar's second example (i.e. "remove all datetimes within 2 seconds of the previous datetime") is the desired outcome, I'd do:
>> dt = sort(dt);
>> dt([true; diff(dt) >= seconds(2)]) % the leading TRUE accounts for the first element offset

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

### 回答 (1 件)

Jalaj Gambhir 2020 年 5 月 18 日

Hi,
The following is one of the way in which you can achieve your task, as pointed out in the comment, "remove all datetimes within 2 seconds of the previous datetime"
DateStrings = {'14-May-2020 10:47:18','14-May-2020 10:47:19','14-May-2020 10:47:20','14-May-2020 10:53:36','14-May-2020 10:54:05', '14-May-2020 10:51:03', '14-May-2020 10:51:05'};
t = datetime(DateStrings,'InputFormat','dd-MMM-yyyy HH:mm:ss');
t = sort(t);
for i=1:length(t)
if ~isnat(t(i))
lower = t(i)+seconds(1);
upper = lower + seconds(2);
t(isbetween(t,lower,upper)) = NaT;
end
end
result = t(~isnat(t));
Hope this helps!

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

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by