dividing a distance double by a duration time to get rate
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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;
0 Commenti
Risposta accettata
dpb
il 30 Lug 2022
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 Commenti
Steven Lord
il 2 Ago 2022
You don't need to use "magic numbers" like 3600 and 24.
N = datetime('now') % Right now
MID = datetime('today') % The previous midnight
howLongSinceMidnight = N - MID % A duration
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)
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))
86,400 seconds in a day.
minutesSinceMidnight = minutes(howLongSinceMidnight)
This tracks because there are:
minutesPerDay = minutes(days(1))
1440 minutes in a day.
hoursSinceMidnight = hours(howLongSinceMidnight)
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
mpm = m / minutesSinceMidnight % miles per minute
mps = m / secondsSinceMidnight % miles per second
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Time Series Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!