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 个评论

What does n represent? What are you expecting the output to be?
n is a number of lectures, and I expected to obtain a Y which should be the number of lectures normalized by time (t).

请先登录,再进行评论。

 采纳的回答

dpb
dpb 2016-12-1

1 个投票

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 个评论

Can you give me an example in matlab code to do this, please?
t='00:01:12';
formatIn='HH:MM:SS';
[Y, M, D, H, MN, S] = datevec(t,formatIn);
t_in_seconds=3600*H+60*MN+S;
Best wishes
Torsten.
dpb
dpb 2016-12-1
编辑:dpb 2016-12-1
I don't have a release incorporating the datetime class but given the examples in the doc's, I'd think you should be able to just use the value itself in conjunction with the display format and get the right answer...but I can't test it here to find out for sure. As Torsten shows, there's always the brute-force technique altho again with duration I'd think there'd be a builtin method but I find the documentation alone quite incomplete and confusing as to what the behavior really is...
ADDENDUM Ah, ok, I finally found it...indeed, for a duration object you do needs must convert; it isn't smart-enough to return the numerically correct value simply based on format property as I'd hope...use appropriate units function for your desired scaling as per <Convert between datetime arrays and numbers>
Thank you very much!!!!!
Michela, instead of using datevec, just convert the duration to the units that you want. If you want lectures per hour, do this
n / hours(t)
If you want lectures per day, do this:
n / days(t)
If you're looking for "lectures per month", you could probably use a calendar duration to do that in essentially the same way, but defining that is conceptually tricky because months are different lengths.
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 2016-12-1
编辑:dpb 2016-12-1
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by