Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!news-fra1.dfn.de!news0.de.colt.net!news3.fe.internet.bosch.com!news4.fe.internet.bosch.com!not-for-mail
From: "Andrea Schmidt" <external.Andrea.Schmidt@de.bosch.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to vectorize and avoid this for loop to speed up?
Date: Wed, 25 Jul 2007 07:38:01 +0200
Organization: Bosch Group
Lines: 53
Message-ID: <f86njq$qf0$1@news4.fe.internet.bosch.com>
References: <f86ief$f4n$1@news.Stanford.EDU>
NNTP-Posting-Host: bue22531.bue.de.bosch.com
X-Trace: news4.fe.internet.bosch.com 1185341882 27104 10.45.106.153 (25 Jul 2007 05:38:02 GMT)
X-Complaints-To: news@news4.fe.internet.bosch.com
NNTP-Posting-Date: Wed, 25 Jul 2007 05:38:02 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1807
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807
Xref: news.mathworks.com comp.soft-sys.matlab:420905



Hello,

I don't know if it is faster, but it is without a for-loop:

tmp=exp(-us*a-us*sum(r)-dot(r,b*p));  %dot is dot-product

I hope that helps you.
Best regards
Andrea

"Linus Utopia" <linus_utopia@gmail.com> schrieb im Newsbeitrag
news:f86ief$f4n$1@news.Stanford.EDU...
> Hi all,
>
> When inside a loop, there is a vector operator(r and p) already, how to
> further vectorize it along another dimension (for "u" and "us")?
>
> Here is my code:
>
> ---------------
>
> tmp=zeros(size(us));  %pre-allocation
> for i=1:length(us);
>     u=us(i);
>     t7=u+b*p(:);
>     tmp(i)=exp(-u*a- r(:).'*t7(:) );
> end;
>
> ---------------
>
> Let me explain. "us" is a vector. I generate "tmp" the same length as
"us".
>
> For each element of "us", I compute an element for "tmp".
>
> "p" and "r" are vectors of equal length.
>
> "a" and "b" are scalars and constants.
>
> r(:).'*t7(:)  is the dot-product of two vectors and hence it is a scalar.
>
> Hence each tmp(i) is a scalar.
>
> Is there a way to vectorize the above for loop?
>
> I have been pondering on this for long time but I am not that creative, or
> perhaps is lack of experience, etc. I couldn't come up with a good idea...
>
> Thanks!
>
>