Pairwise difference of datetime vectors (i.e. pdist for time data)

1 回表示 (過去 30 日間)
Alex G
Alex G 2019 年 7 月 1 日
コメント済み: dpb 2019 年 7 月 3 日
I have two datetime vectors of differing lengths (500x1 and 15000x1) and was wondering how to produce a 500x15000 matrix of the differences between these times. I've previously used pdist2() to answer this question for double vectors, but can't find the function to do it for datetimes.

採用された回答

Peter Perkins
Peter Perkins 2019 年 7 月 2 日
編集済み: Peter Perkins 2019 年 7 月 2 日
pdist is probably much more than you actually need, assuming that by "distance" you mean a simple subtraction. pdist is designed for pairwise diatances between vectors, using one of several distance measures. It will do what you want, but is kind of overkill. I think what you are looking for is what's referred to as "implicit expansion", a more elegant behavior that addresses the same cases as bsxfun used to be used for:
>> (1:3) - (1:4)'
ans =
0 1 2
-1 0 1
-2 -1 0
-3 -2 -1
I forget when this was added; a few years ago IIRC. In any case, you are right that datetime does not yet support it. But it's easy to do by hand, using ndgrid:
>> d1 = datetime(2019,7,1,0:4,0,0);
>> d2 = datetime(2019,7,1,0:3,0,30);
>> [D1,D2] = ndgrid(d1,d2)
D1 =
5×4 datetime array
01-Jul-2019 00:00:00 01-Jul-2019 00:00:00 01-Jul-2019 00:00:00 01-Jul-2019 00:00:00
01-Jul-2019 01:00:00 01-Jul-2019 01:00:00 01-Jul-2019 01:00:00 01-Jul-2019 01:00:00
01-Jul-2019 02:00:00 01-Jul-2019 02:00:00 01-Jul-2019 02:00:00 01-Jul-2019 02:00:00
01-Jul-2019 03:00:00 01-Jul-2019 03:00:00 01-Jul-2019 03:00:00 01-Jul-2019 03:00:00
01-Jul-2019 04:00:00 01-Jul-2019 04:00:00 01-Jul-2019 04:00:00 01-Jul-2019 04:00:00
D2 =
5×4 datetime array
01-Jul-2019 00:00:30 01-Jul-2019 01:00:30 01-Jul-2019 02:00:30 01-Jul-2019 03:00:30
01-Jul-2019 00:00:30 01-Jul-2019 01:00:30 01-Jul-2019 02:00:30 01-Jul-2019 03:00:30
01-Jul-2019 00:00:30 01-Jul-2019 01:00:30 01-Jul-2019 02:00:30 01-Jul-2019 03:00:30
01-Jul-2019 00:00:30 01-Jul-2019 01:00:30 01-Jul-2019 02:00:30 01-Jul-2019 03:00:30
01-Jul-2019 00:00:30 01-Jul-2019 01:00:30 01-Jul-2019 02:00:30 01-Jul-2019 03:00:30
>> delta = D1 - D2
delta =
5×4 duration array
-00:00:30 -01:00:30 -02:00:30 -03:00:30
00:59:30 -00:00:30 -01:00:30 -02:00:30
01:59:30 00:59:30 -00:00:30 -01:00:30
02:59:30 01:59:30 00:59:30 -00:00:30
03:59:30 02:59:30 01:59:30 00:59:30
That makes two temporary arrays as big as the result, but however you do this, you will end up with a 15000x500 array as your result (unless I've misunderstood).
  1 件のコメント
dpb
dpb 2019 年 7 月 3 日
Good point, Peter! I didn't really think about the calculation itself as just thinking of how to get around the input data type mismatch for OP...

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

その他の回答 (1 件)

dpb
dpb 2019 年 7 月 2 日
Presuming not excessive precision, one could convert to datenum, compute distances and then return to datetime
Seems like a feature enhancement.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by