Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fast calculation of velocity from x & y data

Subject: Fast calculation of velocity from x & y data

From: Bryan Benson

Date: 28 Apr, 2010 18:19:05

Message: 1 of 8

Hi,

I searched but couldn't find an answer to this.

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:

clear hypotenuse
for i=1:length(t)
    hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);
end

clear rawvelocity
rawvelocity(1)=0;
for i=2:length(hypotenuse)
    rawvelocity(i) = hypotenuse(i)-hypotenuse(i-1);
end

The time steps are all equal, so they don't need to be factored into the calculation here.

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.

Thank you!

Subject: Fast calculation of velocity from x & y data

From: John D'Errico

Date: 28 Apr, 2010 18:30:25

Message: 2 of 8

"Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...
> Hi,
>
> I searched but couldn't find an answer to this.
>
> 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:
>
> clear hypotenuse
> for i=1:length(t)
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);
> end
>
> clear rawvelocity
> rawvelocity(1)=0;
> for i=2:length(hypotenuse)
> rawvelocity(i) = hypotenuse(i)-hypotenuse(i-1);
> end
>
> The time steps are all equal, so they don't need to be factored into the calculation here.
>
> Because the x&y data columns are long (over 100,000 positions), MATLAB will not let me preallocate an array.

Horse hockey.

Of course matlab will let you preallocate the 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.
>

Preallocate the array. See my comment above.

But even better, DON'T use a loop at all!

hypotenuze = sqrt(x.^2 + y.^2);

John

Subject: Fast calculation of velocity from x & y data

From: Walter Roberson

Date: 28 Apr, 2010 18:44:09

Message: 3 of 8

Bryan Benson wrote:
> Hi,
>
> I searched but couldn't find an answer to this.
>
> 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:
>
> clear hypotenuse
> for i=1:length(t)
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);
> end
>
> clear rawvelocity
> rawvelocity(1)=0;
> for i=2:length(hypotenuse)
> rawvelocity(i) = hypotenuse(i)-hypotenuse(i-1);
> end
>
> The time steps are all equal, so they don't need to be factored into the
> calculation here.
>
> 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.

It might be interesting to try

rawvelocity = diff(abs(complex(x,y)));


John is right that it is better just to vectorize instead of to pre-allocate,

rawvelocity = diff(sqrt(x.^2 + y.^2));


Note that if your values might be above 10^150 or below 10^(-150) then instead
of using sqrt(x.^2 + y.^2) you should use hypot(x, y) which is an undocumented
function that will be slower but will produce accurate answers without
overflows or underflows from squaring the values.


If you get stuck and it complains about running out of memory, you can operate
in chunks: the hypot values for (say) index 10000 to 19999 are independent of
the ones from index 1 to 9999, and even the diff step is independent except
right at the boundary where you join the pieces.

Subject: Fast calculation of velocity from x & y data

From: Roger Stafford

Date: 28 Apr, 2010 19:06:04

Message: 4 of 8

"Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...
> ........
> for i=1:length(t)
> hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);
> end
>
> rawvelocity(1)=0;
> for i=2:length(hypotenuse)
> rawvelocity(i) = hypotenuse(i)-hypotenuse(i-1);
> end
> ........
------------------
  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(i-1) is *not* the distance traveled between time i-1 and time i. Far from it! Draw a triangle connecting the three points (0,0), (x(i-1),y(i-1)), 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!

  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.

  Either way the calculation is not correct as far as I can see.

Roger Stafford

Subject: Fast calculation of velocity from x & y data

From: Bryan Benson

Date: 28 Apr, 2010 19:11:04

Message: 5 of 8

> Horse hockey.
>
> Of course matlab will let you preallocate the array.

It gives me a 'too many elements' error message

But thank you for the other tip, that does make it much faster!

Subject: Fast calculation of velocity from x & y data

From: Bruno Luong

Date: 28 Apr, 2010 19:18:06

Message: 6 of 8

"Bryan Benson" <do.not.email@spam.com> wrote in message <hra188$ev7$1@fred.mathworks.com>...

>
> It gives me a 'too many elements' error message

Beware about the difference between

hypothenus = zeros(length(x))
hypothenus = zeros(numel(x))
hypothenus = zeros(100000)

and

hypothenus = zeros(1,length(x))
hypothenus = zeros(1,numel(x))
hypothenus = zeros(1,100000)
hypothenus = zeros(size(x))

Bruno

Subject: Fast calculation of velocity from x & y data

From: Bryan Benson

Date: 28 Apr, 2010 19:29:05

Message: 7 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hra0us$p6h$1@fred.mathworks.com>...
> "Bryan Benson" <do.not.email@spam.com> wrote in message <hr9u6p$kl3$1@fred.mathworks.com>...
> > ........
> > for i=1:length(t)
> > hypotenuse(i) = sqrt(x(i)^2 + y(i)^2);
> > end
> >
> > rawvelocity(1)=0;
> > for i=2:length(hypotenuse)
> > rawvelocity(i) = hypotenuse(i)-hypotenuse(i-1);
> > end
> > ........
> ------------------
> 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(i-1) is *not* the distance traveled between time i-1 and time i. Far from it! Draw a triangle connecting the three points (0,0), (x(i-1),y(i-1)), 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!
>
> 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.
>
> Either way the calculation is not correct as far as I can see.
>
> Roger Stafford

Hi Roger,

We're only interested in velocity away from the origin. But good eye!

Bruno: thank you! That removed the error message.

Subject: Fast calculation of velocity from x & y data

From: James Tursa

Date: 28 Apr, 2010 19:30:23

Message: 8 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <hr9vm1$ev8$1@canopus.cc.umanitoba.ca>...
>
> Note that if your values might be above 10^150 or below 10^(-150) then instead
> of using sqrt(x.^2 + y.^2) you should use hypot(x, y) which is an undocumented
> function that will be slower but will produce accurate answers without
> overflows or underflows from squaring the values.

hypot is documented, at least in later versions of MATLAB.

James Tursa

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us