how to calculate the elapsed time between rows
1 view (last 30 days)
Show older comments
Angeliki Efstathiou
on 31 Aug 2014
Commented: Angeliki Efstathiou
on 31 Aug 2014
Hi, I have a 11619x6 matrix of the form [No.Data, YY, mm, dd, hour, sec]
1 1968 1 1 7 50 50.76
2 1968 1 4 11 37 8.190
3 1968 1 11 23 17 0.1400
4 1968 1 14 21 21 59.06
5 1968 1 17 19 34 55.70
6 1968 1 18 21 22 32.22
etc...
and I want to calculate the elapsed time between the 1st row and the 2nd, and then between the 1st row and the 3rd, then between the 1st row and the 4th and so on...
My thought was to do something simple without using a for loop
T= x(1:end-1,2:7);
Tn= x(2:end,2:7);
dt=etime(Tn,T);
but it didn't work. Can anyone give me an idea how to solve this?
Thanks!
0 Comments
Accepted Answer
Star Strider
on 31 Aug 2014
Edited: Star Strider
on 31 Aug 2014
This works:
M = [1 1968 1 1 7 50 50.76
2 1968 1 4 11 37 8.190
3 1968 1 11 23 17 0.1400
4 1968 1 14 21 21 59.06
5 1968 1 17 19 34 55.70
6 1968 1 18 21 22 32.22];
MT = M(:,2:7); % Time Vectors
for k1 = 2:size(MT,1)
MET(k1,:) = etime(MT(k1,:), MT(1,:)); % Vector of Elapsed Times
end
You can do it without a loop, but you have to provide a matrix of MT(1,:) values equal in length to the rest of the time vectors:
MT1 = repmat(MT(1,:), size(MT,1)-1, 1); % Matrix of ‘MT(1,:)’ Values
MET = etime(MT(2:end,:), MT1);
The loop takes about 8% longer than the repmat version in this example.
2 Comments
More Answers (1)
dpb
on 31 Aug 2014
Can't just subtract times in hr,min,sec form since there's rollover. Convert to datenums and subtract them then convert back to whatever units desired...
>> dn=datenum(t(:,2:end)); % convert to datenums
>> dt=dn(2:end)-dn(1) % compute since first (days.fractions_thereof)
dt =
3.1571
10.6432
13.5633
16.4889
17.5637
>> dt*24 % and difference in hours
ans =
75.7715
255.4359
325.5190
395.7347
421.5282
>>
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!