Path: news.mathworks.com!not-for-mail
From: "Titus" <titus.edelhofer@mathworks.de>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorization help
Date: Tue, 20 Nov 2007 13:14:35 +0100
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <fhuj3a$er4$1@fred.mathworks.com>
References: <26726115-2bc1-4fc2-8ce2-7b5a4d1178a1@b40g2000prf.googlegroups.com>
NNTP-Posting-Host: de-edelhoft-x.ac.mathworks.de
X-Trace: fred.mathworks.com 1195560874 15204 172.16.75.150 (20 Nov 2007 12:14:34 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 20 Nov 2007 12:14:34 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
Xref: news.mathworks.com comp.soft-sys.matlab:438497




"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