Clear Filters
Clear Filters

Divide a number by time

11 views (last 30 days)
Michela Longhi
Michela Longhi on 1 Dec 2016
Edited: dpb on 1 Dec 2016
Hi,
I have a time express in duration
t =
duration
00:01:12
and I want to divide a number n=9 by t: Y=n/t
But how can I do it? I should convert the time duration in a number as 1,12 and then divide?
Help me, please
  2 Comments
Brendan Hamm
Brendan Hamm on 1 Dec 2016
What does n represent? What are you expecting the output to be?
Michela Longhi
Michela Longhi on 1 Dec 2016
n is a number of lectures, and I expected to obtain a Y which should be the number of lectures normalized by time (t).

Sign in to comment.

Accepted Answer

dpb
dpb on 1 Dec 2016
Convert the time to lowest common interval; looks like seconds and divide (or, of course, can use whatever time unit desired; mph would be a common US unit for some such calculations but that can be done post facto, too).
  7 Comments
Peter Perkins
Peter Perkins on 1 Dec 2016
dpb, a duration represents a fixed length of time, independently of the units in which it's displayed in. So for example
>> isequal(hours(1),minutes(60),seconds(3600))
ans =
1
datetime is the same way about time zones, these two datetimes have different displays but their values are equal:
>> ny = datetime(2016,12,1,12,0,0,'TimeZone','America/New_York')
ny =
01-Dec-2016 12:00:00
>> la = datetime(2016,12,1,9,0,0,'TimeZone','America/Los_Angeles')
la =
01-Dec-2016 09:00:00
>> isequal(ny,la)
ans =
1
There is no double method to do duration->numeric conversion, because it would either have to use some default unit, or (as you suggest) the number that it returns would depend on the display format. Either would be kind of confusing. So to convert duration to numeric, you specify what units you want that number to be in.
dpb
dpb on 1 Dec 2016
Edited: dpb on 1 Dec 2016
Peter, that's well and good (albeit probably not the way I'd've designed it :) ) but finding it out from the doc is pretty tough...it took me some several minutes of searching links to finally uncover the above page...but the example you give above indicates that the user has to know what the units of the value are; to write the above has the inherent assumption of the numbers are in seconds. I was figuring the general case is the same excepting if one has generated a duration by some mechanism, the magnitude (generally) will have been set by that generation process such as subtracting an origin from a time series or the like. In that case the return value if specify a given format being (for your example above) 1,60,3600 depending on 'hours','minutes','seconds' as the format wouldn't seem that unexpected to me; if fact, not having the class to play with to learn firsthand differently, it seems to me it would be the expected result for assignment just as what you see on display.

Sign in to comment.

More Answers (0)

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!