|
"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
|