Problem 43667. Use a timetable to analyze a train timetable (Part 4)
You are analyzing a train timetable (you have some time to kill since you just missed your train!) What is the average daily trip duration?
Example:
DepTime = datetime(2016,11,1,[6 8 9],[22 51 05],0)';
ArrTime = datetime(2016,11,1,[9 10 12],[17 32 09],0)';
Destination = categorical({'BOS';'NYC';'BOS'});
tt = timetable(DepTime,Destination,ArrTime)
tt =
DepTime Destination ArrTime
____________________ ___________ ____________________
01-Nov-2016 06:22:00 BOS 01-Nov-2016 09:17:00
01-Nov-2016 08:51:00 NYC 01-Nov-2016 10:32:00
01-Nov-2016 09:05:00 BOS 01-Nov-2016 12:09:00
The result should be a duration variable:
Dur = [duration(3,9,05);duration(2,55,15);duration(3,0,24)]
Dur =
3×1 duration array
03:09:50
02:55:15
03:00:24
Feature Tip: R2016b introduces timetables with related functions which may be helpful. To learn more see MATLAB Timetables.
Related Problems:
Use a timetable to analyze a train timetable (Part 4)
Solution Stats
Problem Comments
-
4 Comments
Show
1 older comment
Nikolaos Nikolaou
on 28 Oct 2019
Peng do share the grouping method cause it eludes me :)
Rafael S.T. Vieira
on 30 Aug 2020
There seems to be an error with the test suite: the first mean time should be 03:09:50 and not 03:09:05. And the test code should probably be abs(ttDur - Dur) <= duration([0 0 1]) (not 0.01).
goc3
on 22 Apr 2022
The test suite has been updated to use 03:09:50. Also, the check for each of the three average durations is made against 0.0001 now, instead of 0.01, to ensure 03:09:05 will not pass (as that was incorrect). Finally, the test suite now accommodates a row vector (in addition to the expected column vector) as the function output.
Solution Comments
Show commentsProblem Recent Solvers60
Suggested Problems
-
2966 Solvers
-
483 Solvers
-
748 Solvers
-
343 Solvers
-
321 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!