For Programmers: Free Programming Magazines  


Home > Archive > Matlab > December 2006 > How do I optimise this?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I optimise this?
Enter The

2006-12-27, 4:19 am

Hi,

I'm a fairly new Matlab user and am trying to optimise the following
code as it's very slow. Could anyone here help, or point me in the
right direction for assistance?

ball is a 20,000 x 6 matrix

for i=1:size(ball,1)

vector_diff = ball - ones(series_length,1) * ball(i,:) ;
distances = sqrt(sum(vector_diff.^2,2));

%then do some other stuff with distances
end

Thanks,

Enter.

Roger Stafford

2006-12-27, 7:09 pm

In article <1167209721.224256.18470@f1g2000cwa.googlegroups.com>, "Enter
The" <enterthe@walla.com> wrote:

> Hi,
>
> I'm a fairly new Matlab user and am trying to optimise the following
> code as it's very slow. Could anyone here help, or point me in the
> right direction for assistance?
>
> ball is a 20,000 x 6 matrix
>
> for i=1:size(ball,1)
>
> vector_diff = ball - ones(series_length,1) * ball(i,:) ;
> distances = sqrt(sum(vector_diff.^2,2));
>
> %then do some other stuff with distances
> end
>
> Thanks,
>
> Enter.

------------------
Until you show us what "some other stuff with distances" is comprised
of, we can do little to help you. You are going through the for-loop
twenty thousand times and each time you generate a vector twenty thousand
elements long. Then you overwrite it the next time through. That is
bound to be a time consuming process unless a method can be devised to
shorten the "other stuff".

Roger Stafford
Randy Poe

2006-12-27, 7:09 pm


Enter The wrote:
> Hi,
>
> I'm a fairly new Matlab user and am trying to optimise the following
> code as it's very slow. Could anyone here help, or point me in the
> right direction for assistance?
>
> ball is a 20,000 x 6 matrix
>
> for i=1:size(ball,1)
>
> vector_diff = ball - ones(series_length,1) * ball(i,:) ;


You might try REPMAT here and see if it performs any better.

vector_diff = ball - repmat(ball(i,:), series_length, 1);

> distances = sqrt(sum(vector_diff.^2,2));
>
> %then do some other stuff with distances
> end
>


Otherwise, what you really ought to do is run the profiler and
see if, as Roger suggests, it's the "other stuff" that is
taking up your time.

HELP PROFILE

- Randy

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com