Matrix math with Datetime arrays
조회 수: 8 (최근 30일)
이전 댓글 표시
I have a function for determining the element in Array 1 which is closest to a given element in Array 2, developed form this question & answer chain. However, when I attempt to input datetime arrays, the segment [Array2.'-Array1] throws an error stating that the inputs have to be the same size, which is not a fault thrown when I feed the code differently-sized numeric arrays. I would use time2num to convert the datetime arrays, however that function does not allow for the level of precision I require (less than seconds).
Are there any known work-arounds for performing matrix math on datetime arrays or, failing that, any other ideas on how to convert datetimes to numeric values at arbitrary precisions?
댓글 수: 0
채택된 답변
Steven Lord
2021년 11월 3일
Support for implicit expansion for certain operations on datetime, duration, calendarDuration, and categorical arrays was added in release R2020b.
If upgrading is not an option, you could use repmat to convert the vectors of different orientation into matrices with the same dimensions and perform the elementwise operations on those matrices.
댓글 수: 0
추가 답변 (1개)
Kelly Kearney
2021년 11월 3일
As you discovered, implicit expansion of is only supported for numerical arrays (though I'm not sure where that's documented.) To do the same with datetimes, I would just convert to datenumbers:
t1 = datetime(2021,1:12,1);
t2 = datetime(2021,1,1) + days(rand(10,1)*365);
dt = datenum(t1) - datenum(t2); % pairwise difference in days (numeric array)
You can always cast back to durations if you need to do time-oriented stuff after that:
dt = days(dt); % duration array
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!