Thread Subject: How to vectorize and avoid this for loop to speed up?

Subject: How to vectorize and avoid this for loop to speed up?

From: Linus Utopia

Date: 25 Jul, 2007 00:07:41

Message: 1 of 6

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!

Subject: How to vectorize and avoid this for loop to speed up?

From: Andrea Schmidt

Date: 25 Jul, 2007 07:38:01

Message: 2 of 6

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

Subject: How to vectorize and avoid this for loop to speed up?

From: Linus Utopia

Date: 25 Jul, 2007 22:28:44

Message: 3 of 6

Hi Andrea,

I am sorry but I forgot to add more details: Please see below. Here there
was a "log" missing in the definition of t7. And c is a scalar constant...

>> tmp=zeros(size(us)); %pre-allocation
>> for i=1:length(us);
>> u=us(i);
>> b=1/(u+c);
>> t7=log(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.



"Andrea Schmidt" <external.Andrea.Schmidt@de.bosch.com> wrote in message
news:f86njq$qf0$1@news4.fe.internet.bosch.com...
> 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!
>>
>>
>
>

Subject: How to vectorize and avoid this for loop to speed up?

From: last) (first then

Date: 26 Jul, 2007 03:24:23

Message: 4 of 6

What is c?

Subject: How to vectorize and avoid this for loop to speed up?

From: last) (first then

Date: 26 Jul, 2007 03:26:33

Message: 5 of 6

Looks like you oughta do element by element matrix multiplication ...

.* a couple of n x m matrices together to create another n x m matrix.

Subject: How to vectorize and avoid this for loop to speed up?

From: Linus Utopia

Date: 26 Jul, 2007 07:29:04

Message: 6 of 6


"last) (first then" <nospam@nospamplease.com> wrote in message
news:f89499$aul$1@fred.mathworks.com...
> Looks like you oughta do element by element matrix multiplication ...
>
> .* a couple of n x m matrices together to create another n x m matrix.

Sure I know how to do this. But how to do this efficiently.
That's what I want to learn... If I don't keep learning creative things, I
will be sticking to my naive implementation with no improvment.

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorize Ned Gulley 25 Jul, 2007 23:39:19
rssFeed for this Thread

Contact us at files@mathworks.com