difference between hours, minutes and seconds

1 回表示 (過去 30 日間)
elisa ewin
elisa ewin 2017 年 2 月 2 日
回答済み: Steven Lord 2017 年 2 月 2 日
Hi!
I have matrix A
M=[8 8 48;8 15 29;9 30 15;9 48 53;9 49 30;9 52 12;9 57 0;10 2 27;10 28 52;11 29 47;11 43 41;11 44 4];
The first column of the matrix indicates hours, the second minutes and the third seconds. I want to calculate the difference between consecutive rows
Eg.
M(2,1:3)-M(1,1:3) = 0 6 41
Can you help me? Thanks

採用された回答

Star Strider
Star Strider 2017 年 2 月 2 日
See if this does what you want:
M=[8 8 48;8 15 29;9 30 15;9 48 53;9 49 30;9 52 12;9 57 0;10 2 27;10 28 52;11 29 47;11 43 41;11 44 4];
Mdn = datenum([repmat([2017 02 02], size(M,1), 1) M]); % Date Numbers
Mdndif = datevec(diff(Mdn)); % Date Number Differences As Date Vectors
Out = Mdndif(:, 4:end) % Desired Result
Out =
0 6 41
1 14 46
0 18 38
0 0 37
0 2 42
0 4 48
0 5 27
0 26 25
1 0 55
0 13 54
0 0 23

その他の回答 (2 件)

Jan
Jan 2017 年 2 月 2 日
編集済み: Jan 2017 年 2 月 2 日
D = diff(M, [], 1);
DinSec = D * [3600; 60; 1] / 86400; % difference in seconds
S = datevec(DinSec);
Result = M(:, 4:6)

Steven Lord
Steven Lord 2017 年 2 月 2 日
Use a duration object.
% Sample data
M = [ 8 8 48; 8 15 29; 9 30 15; 9 48 53; 9 49 30; 9 52 12; ...
9 57 0; 10 2 27; 10 28 52; 11 29 47; 11 43 41; 11 44 4];
% Convert from numbers to a duration array
dur = duration(M);
% Compute the DIFFerence between the rows of the duration array
Ddur = diff(dur, [], 1);
% Extract the hour, minute, and second data from Ddur
% and store that data in the matrix DM
[DM(:, 1), DM(:, 2), DM(:, 3)] = hms(Ddur);
% Show the result
disp(DM)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by