difference of datetimes using the "diff" function
조회 수: 4 (최근 30일)
이전 댓글 표시
I would like to find the temporal difference between the elements in the first (left) column and the elements in the second (right) column. I have tried the diff function, but it gives me an unexpected result. How can I still use the diff function correctly ?
% input
a = datetime({'2022-05-17 05:57:11.568' '2022-05-17 06:01:23.552'
'2022-05-17 06:01:23.552' '2022-05-17 06:02:32.760'
'2022-05-17 06:02:32.760' '2022-05-17 06:03:57.660'
'2022-05-17 06:03:57.660' '2022-05-17 06:19:28.740'
'2022-05-17 06:19:28.740' '2022-05-17 06:20:50.100'
'2022-05-17 06:20:50.100' '2022-05-17 06:21:49.408'
'2022-05-17 06:21:49.408' '2022-05-17 06:23:15.980'
'2022-05-17 06:23:15.980' '2022-05-17 06:24:22.200'})
% If I use "diff(a)", why do I get two columns of "differences", instead of only one ?
diff(a)
% the following command looks like to give the correct result (but I would like to use "diff" if possible)
abs(a(:,1)-a(:,2))
댓글 수: 2
Stephen23
2022년 10월 20일
"why do I get two columns of "differences", instead of only one ?"
The DIFF documentation explains this. The documentation states "diff(X) calculates differences between adjacent elements of X along the first array dimension whose size does not equal 1..." and it then procedes to give a detailed explanation of the returned sizes for vectors and matrices: "If X is a nonempty, nonvector p-by-m matrix, then Y = diff(X) returns a matrix of size (p-1)-by-m, whose elements are the differences between the rows of X"
So because you did not specify the dimension argument and the first dimension of your matrix is non-scalar then DIFF operates along the first dimension. So far everything you show is exactly as documented and expected.
답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!