Thread Subject: vectorization help

Subject: vectorization help

From: KC

Date: 20 Nov, 2007 06:15:33

Message: 1 of 3

This code takes for ever (well not really, but longer than I think it
should) to run... I can't figure out how to speed it up... any help?
I'm trying to learn vectorization techniques, but I don't see how
vectorization can be applied here. Can it?

%% Define angle vectors
x = avgang(:,1);
y = avgang(:,2);
[r,c]=size(angs{1,1});
%% loop
for n = 1:r;
dist(:,n) = sqrt((x-angs{1,1}(n,1)).^2 + (y-angs{1,2}(n,1)).^2);
[minDist(n,1), minDist(n,2)] = min(distVectors(:,n));
%second column is the index number
end

FYI, "angs" is a 1x3 cell (but I only care about cells 1 & 2 for now),
and each cell is a 1001x9 double. "avgang" is a 1001x3 array, and I
only care about columns 1 & 2 for now.

Thanks for any help.
-Kieran

Subject: vectorization help

From: Titus

Date: 20 Nov, 2007 12:14:35

Message: 2 of 3


"KC" <kc_news@sonic.net> schrieb im Newsbeitrag
news:26726115-2bc1-4fc2-8ce2-7b5a4d1178a1@b40g2000prf.googlegroups.com...
> This code takes for ever (well not really, but longer than I think it
> should) to run... I can't figure out how to speed it up... any help?
> I'm trying to learn vectorization techniques, but I don't see how
> vectorization can be applied here. Can it?
>
> %% Define angle vectors
> x = avgang(:,1);
> y = avgang(:,2);
> [r,c]=size(angs{1,1});
> %% loop
> for n = 1:r;
> dist(:,n) = sqrt((x-angs{1,1}(n,1)).^2 + (y-angs{1,2}(n,1)).^2);
> [minDist(n,1), minDist(n,2)] = min(distVectors(:,n));
> %second column is the index number
> end
>
> FYI, "angs" is a 1x3 cell (but I only care about cells 1 & 2 for now),
> and each cell is a 1001x9 double. "avgang" is a 1001x3 array, and I
> only care about columns 1 & 2 for now.
>
> Thanks for any help.
> -Kieran

Hi Kieran,
more effectively then thinking too hard about vectorization is
preallocation:
add before the loop:
dist = zeros(length(x), r);
minDist = zeros(r, 2);
This should speed up your code, since MATLAB need not resize the
two matrices in each loop.

Titus

Subject: vectorization help

From: Pete

Date: 20 Nov, 2007 17:14:22

Message: 3 of 3

Hi,
I had a quick test and Titus' suggestion of preallocation makes a huge impact on
the length of time it takes and you probably don't need anything else.

If you really want to vectorize it, I *think* you can take the last bit out of the
loop and have instead
[minDist(:,1), minDist(:,2)] = min(distVectors);

To vectorize the rest, the only way I can think of would involve creating huge
matrices with repmat and I'm not sure if the advantages would be lost because
of needing more memory and making the code harder to read.

Instead of your big sqrt line, you could also probably use hypot.

Though I'm relatively new to MATLAB, so I wouldn't trust anything I suggest
without testing it...

Pete

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com