dividing a distance double by a duration time to get rate

조회 수: 14 (최근 30일)
Mike Susedik
Mike Susedik 2022년 7월 29일
댓글: Mike Susedik 2022년 8월 26일
I just want to divide distance, meters by time, seconds. You can't divide a double by a duration. I start with two datenums, subtract 2 them from eachother, use seconds to convert the difference to seconds, then do I have to apply seconds again? seconds(seconds(datenum))? Seems to work, but..
Error using ./
Cannot divide a double array by a duration array.
Error in dopplerImpact (line 144)
dopplerVelocity = rangeDelta./timeDelta;

채택된 답변

dpb
dpb 2022년 7월 30일
A datenum is a double; you must mean a datetime -- subtracting them does create a duration type/class, yes. There are some warts with using datetime class for other than calendar functionality like signal processing as you've discovered.
But, you should only have to apply seconds once -- probably when you create the time vector would be the place to do so...
Illustration with the types since you don't show actual code that you used to see the exact sequence...
t=linspace(0,2*pi).'; % a double time vector with which to
v=sin(t); % from which to compute a velocity
d=cumsum(v); % and a distance (doubles)
t=seconds(t); % turn the t vector into a duration (equivalent to your subtraction)
plot(t,[v d]) % show the results for grins...
Now try to compute a velocity from the d, t vectors as you did --
>> v=d./t;
Cannot divide a double array by a duration array.
>>
and get same problem (to show did duplicate your situation).
Now, convert
v=d./seconds(t); % convert the duration to double
The function <seconds> is a chameleon -- it tranforms numeric data to a duration of the number of seconds; otoh, it converts a duration quantity back to numeric double of the seconds in the duration variable.
If you have datetime time data, the shortest way for normal signal processing type calculations would be
dt=seconds(t-t(1)); % subtract initial time; convert result to double seconds
  댓글 수: 3
Steven Lord
Steven Lord 2022년 8월 2일
You don't need to use "magic numbers" like 3600 and 24.
N = datetime('now') % Right now
N = datetime
02-Aug-2022 23:11:21
MID = datetime('today') % The previous midnight
MID = datetime
02-Aug-2022
howLongSinceMidnight = N - MID % A duration
howLongSinceMidnight = duration
23:11:21
Now in order to use howLongSinceMidnight in an arithmetic operation with a double, you need to convert it into a double. But there are multiple possible double values that could be equivalent to howLongSinceMidnight.
format longg
secondsSinceMidnight = seconds(howLongSinceMidnight)
secondsSinceMidnight =
83481.03682
Since as I'm typing this it's been a little over 23 hours since midnight, a value of about 83,000 for secondsSinceMidnight seems reasonable. After all there are:
secondsPerDay = seconds(days(1))
secondsPerDay =
86400
86,400 seconds in a day.
minutesSinceMidnight = minutes(howLongSinceMidnight)
minutesSinceMidnight =
1391.35061366667
This tracks because there are:
minutesPerDay = minutes(days(1))
minutesPerDay =
1440
1440 minutes in a day.
hoursSinceMidnight = hours(howLongSinceMidnight)
hoursSinceMidnight =
23.1891768944444
Now which of those numbers would you want to use in your velocity calculations? It depends. Do you want miles per hour, miles per minute, or miles per second? Use hoursSinceMidnight, minutesSinceMidnight, or secondsSinceMidnight respectively. They each represent howLongSinceMidnight, just in different units.
m = 500; % 500 miles
mph = m / hoursSinceMidnight % in miles per hour
mph =
21.561782993677
mpm = m / minutesSinceMidnight % miles per minute
mpm =
0.359363049894617
mps = m / secondsSinceMidnight % miles per second
mps =
0.00598938416491028
Mike Susedik
Mike Susedik 2022년 8월 26일
Great run through Steven, thank you. I think the "magic numbers" are ultimately going away, so this will be helpful to update the code I'm using.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by