在Mathematica中查找最近的时间戳

时间:2017-11-16 17:27:32

标签: wolfram-mathematica nearest-neighbor

在Mathematica中我必须找到最接近给定时间戳的时间戳。我有:

alltrafotstamps = (DateList[#1]) & @@@ reddata[[All, 1]]

什么给出了一个看起来像时间戳列表的东西:

{"2017-11-10 21:36:12.135", "2017-11-10 21:36:50.535",
 "2017-11-10 21:37:28.935", "2017-11-10 21:38:07.335", ...}

现在我这样做:

Nearest[alltrafotstamps, DateList["2017-11-10 22:56:50.535"]]

我收到了这条消息:

Nearest::neard: The default distance function does not give a real numeric distance when applied to the point pair 2017 and 2017-11-10 21:36:12.135.

是不是Nearest不能为时间戳做到这一点,但只能在时间上做到这一点?

1 个答案:

答案 0 :(得分:0)

alltrafotstamps = {
  "2017-11-10 21:36:12.135",
  "2017-11-10 21:36:50.535",
  "2017-11-10 21:37:28.935",
  "2017-11-10 21:38:07.335"};

target = "2017-11-10 21:37:00";

nearest = Nearest[
   AbsoluteTime /@ alltrafotstamps,
   AbsoluteTime[target]];

DateObject @@ nearest

DateList @@ nearest
{2017, 11, 10, 21, 36, 50.535}