From: Bryan Benson on
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!
From: John D'Errico on
"Bryan Benson" <do.not.email(a)spam.com> wrote in message <hr9u6p$kl3$1(a)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
From: Walter Roberson on
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.
From: Roger Stafford on
"Bryan Benson" <do.not.email(a)spam.com> wrote in message <hr9u6p$kl3$1(a)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
From: Bryan Benson on
> 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!
 |  Next  |  Last
Pages: 1 2
Prev: dct2 important
Next: using matlab as a solver