http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751
MATLAB Central Newsreader  Fast calculation of velocity from x & y data
Feed for thread: Fast calculation of velocity from x & y data
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 28 Apr 2010 18:19:05 +0000
Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740251
Bryan Benson
Hi,<br>
<br>
I searched but couldn't find an answer to this.<br>
<br>
I'm running an analysis on x&y data. The program is long, but one section of code takes up the vast majority of the time. Here it is:<br>
<br>
clear hypotenuse<br>
for i=1:length(t)<br>
hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);<br>
end<br>
<br>
clear rawvelocity<br>
rawvelocity(1)=0;<br>
for i=2:length(hypotenuse)<br>
rawvelocity(i) = hypotenuse(i)hypotenuse(i1);<br>
end<br>
<br>
The time steps are all equal, so they don't need to be factored into the calculation here.<br>
<br>
Because the x&y data columns are long (over 100,000 positions), MATLAB will not let me preallocate an array. This section of code takes about 10 minutes to complete! There has to be a faster way, but I'm just not seeing it.<br>
<br>
Thank you!

Wed, 28 Apr 2010 18:30:25 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740257
John D'Errico
"Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...<br>
> Hi,<br>
> <br>
> I searched but couldn't find an answer to this.<br>
> <br>
> I'm running an analysis on x&y data. The program is long, but one section of code takes up the vast majority of the time. Here it is:<br>
> <br>
> clear hypotenuse<br>
> for i=1:length(t)<br>
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);<br>
> end<br>
> <br>
> clear rawvelocity<br>
> rawvelocity(1)=0;<br>
> for i=2:length(hypotenuse)<br>
> rawvelocity(i) = hypotenuse(i)hypotenuse(i1);<br>
> end<br>
> <br>
> The time steps are all equal, so they don't need to be factored into the calculation here.<br>
> <br>
> Because the x&y data columns are long (over 100,000 positions), MATLAB will not let me preallocate an array.<br>
<br>
Horse hockey.<br>
<br>
Of course matlab will let you preallocate the array.<br>
<br>
<br>
> This section of code takes about 10 minutes to complete! There has to be a faster way, but I'm just not seeing it.<br>
> <br>
<br>
Preallocate the array. See my comment above.<br>
<br>
But even better, DON'T use a loop at all!<br>
<br>
hypotenuze = sqrt(x.^2 + y.^2);<br>
<br>
John

Wed, 28 Apr 2010 18:44:09 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740265
Walter Roberson
Bryan Benson wrote:<br>
> Hi,<br>
> <br>
> I searched but couldn't find an answer to this.<br>
> <br>
> I'm running an analysis on x&y data. The program is long, but one <br>
> section of code takes up the vast majority of the time. Here it is:<br>
> <br>
> clear hypotenuse<br>
> for i=1:length(t)<br>
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);<br>
> end<br>
> <br>
> clear rawvelocity<br>
> rawvelocity(1)=0;<br>
> for i=2:length(hypotenuse)<br>
> rawvelocity(i) = hypotenuse(i)hypotenuse(i1);<br>
> end<br>
> <br>
> The time steps are all equal, so they don't need to be factored into the <br>
> calculation here.<br>
> <br>
> Because the x&y data columns are long (over 100,000 positions), MATLAB <br>
> will not let me preallocate an array. This section of code takes about <br>
> 10 minutes to complete! There has to be a faster way, but I'm just not <br>
> seeing it.<br>
<br>
It might be interesting to try<br>
<br>
rawvelocity = diff(abs(complex(x,y)));<br>
<br>
<br>
John is right that it is better just to vectorize instead of to preallocate,<br>
<br>
rawvelocity = diff(sqrt(x.^2 + y.^2));<br>
<br>
<br>
Note that if your values might be above 10^150 or below 10^(150) then instead <br>
of using sqrt(x.^2 + y.^2) you should use hypot(x, y) which is an undocumented <br>
function that will be slower but will produce accurate answers without <br>
overflows or underflows from squaring the values.<br>
<br>
<br>
If you get stuck and it complains about running out of memory, you can operate <br>
in chunks: the hypot values for (say) index 10000 to 19999 are independent of <br>
the ones from index 1 to 9999, and even the diff step is independent except <br>
right at the boundary where you join the pieces.

Wed, 28 Apr 2010 19:06:04 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740269
Roger Stafford
"Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...<br>
> ........<br>
> for i=1:length(t)<br>
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);<br>
> end<br>
> <br>
> rawvelocity(1)=0;<br>
> for i=2:length(hypotenuse)<br>
> rawvelocity(i) = hypotenuse(i)hypotenuse(i1);<br>
> end<br>
> ........<br>
<br>
Bryan, your calculation of velocity looks entirely fallacious to me. If x(i) and y(i) are coordinates of absolute position, then your 'hypotenuse(i)' would be the distance from the coordinate origin at (0,0) to the point (x(i),y(i)). In that case hypotenuse(i)hypotenuse(i1) is *not* the distance traveled between time i1 and time i. Far from it! Draw a triangle connecting the three points (0,0), (x(i1),y(i1)), and (x(i),y(i)). The distance traveled is the length of the side connecting the second two points, which is certainly not the same thing as the difference between the lengths of the other two sides!<br>
<br>
On the other hand if x and y represent differences in successive coordinates, you would want the velocity to be just this 'hypontenuse' not its successive differences.<br>
<br>
Either way the calculation is not correct as far as I can see.<br>
<br>
Roger Stafford

Wed, 28 Apr 2010 19:11:04 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740272
Bryan Benson
> Horse hockey.<br>
> <br>
> Of course matlab will let you preallocate the array.<br>
<br>
It gives me a 'too many elements' error message<br>
<br>
But thank you for the other tip, that does make it much faster!

Wed, 28 Apr 2010 19:18:06 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740274
Bruno Luong
"Bryan Benson" <do.not.email@spam.com> wrote in message <hra188$ev7$1@fred.mathworks.com>...<br>
<br>
> <br>
> It gives me a 'too many elements' error message<br>
<br>
Beware about the difference between<br>
<br>
hypothenus = zeros(length(x))<br>
hypothenus = zeros(numel(x))<br>
hypothenus = zeros(100000)<br>
<br>
and <br>
<br>
hypothenus = zeros(1,length(x))<br>
hypothenus = zeros(1,numel(x))<br>
hypothenus = zeros(1,100000)<br>
hypothenus = zeros(size(x))<br>
<br>
Bruno

Wed, 28 Apr 2010 19:29:05 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740286
Bryan Benson
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hra0us$p6h$1@fred.mathworks.com>...<br>
> "Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...<br>
> > ........<br>
> > for i=1:length(t)<br>
> > hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);<br>
> > end<br>
> > <br>
> > rawvelocity(1)=0;<br>
> > for i=2:length(hypotenuse)<br>
> > rawvelocity(i) = hypotenuse(i)hypotenuse(i1);<br>
> > end<br>
> > ........<br>
> <br>
> Bryan, your calculation of velocity looks entirely fallacious to me. If x(i) and y(i) are coordinates of absolute position, then your 'hypotenuse(i)' would be the distance from the coordinate origin at (0,0) to the point (x(i),y(i)). In that case hypotenuse(i)hypotenuse(i1) is *not* the distance traveled between time i1 and time i. Far from it! Draw a triangle connecting the three points (0,0), (x(i1),y(i1)), and (x(i),y(i)). The distance traveled is the length of the side connecting the second two points, which is certainly not the same thing as the difference between the lengths of the other two sides!<br>
> <br>
> On the other hand if x and y represent differences in successive coordinates, you would want the velocity to be just this 'hypontenuse' not its successive differences.<br>
> <br>
> Either way the calculation is not correct as far as I can see.<br>
> <br>
> Roger Stafford<br>
<br>
Hi Roger,<br>
<br>
We're only interested in velocity away from the origin. But good eye!<br>
<br>
Bruno: thank you! That removed the error message.

Wed, 28 Apr 2010 19:30:23 +0000
Re: Fast calculation of velocity from x & y data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280751#740287
James Tursa
Walter Roberson <roberson@hushmail.com> wrote in message <hr9vm1$ev8$1@canopus.cc.umanitoba.ca>...<br>
> <br>
> Note that if your values might be above 10^150 or below 10^(150) then instead <br>
> of using sqrt(x.^2 + y.^2) you should use hypot(x, y) which is an undocumented <br>
> function that will be slower but will produce accurate answers without <br>
> overflows or underflows from squaring the values.<br>
<br>
hypot is documented, at least in later versions of MATLAB.<br>
<br>
James Tursa