Divide a number by time
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
dpb
2016년 12월 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
Peter Perkins
2016년 12월 1일
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
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!