Thread Subject: vectorize loop exponential

Subject: vectorize loop exponential

From: Rajgopal

Date: 17 Aug, 2009 08:51:02

Message: 1 of 3

Hi All

Any ways to vectorize this loop better

N = 100000

for i = 1 to N
Ymat(:,:,i) = exp(Alphamat + Betamat.*(Xmat(:,:,i))
end

All the matrices are 4*4 matrices.

I tried..

Alphamat1 = repmat(Alphamat,[1 1 N])
Betamat1 = repmat(Betamat, [1 1 N])
Ymat = exp(bsxfun(@plus,Alphamat1,bsxfun(@times,Betamat1,Xmat)))

It didn't really help. Actually it resulted in an increase in time.I tried splitting it up using a temporary variable, but that didn't help either. On profiling, the exp operation is the biggest bottleneck, but I'm hoping vectorizing could reduce the number of calls and thus save time.

Thanks for any suggestions..

Subject: vectorize loop exponential

From: us

Date: 17 Aug, 2009 10:14:02

Message: 2 of 3

"Rajgopal " <runraj@gmail.com> wrote in message <h6b5lm$99t$1@fred.mathworks.com>...
> Hi All
>
> Any ways to vectorize this loop better
>
> N = 100000
>
> for i = 1 to N
> Ymat(:,:,i) = exp(Alphamat + Betamat.*(Xmat(:,:,i))
> end
>
> All the matrices are 4*4 matrices.
>
> I tried..
>
> Alphamat1 = repmat(Alphamat,[1 1 N])
> Betamat1 = repmat(Betamat, [1 1 N])
> Ymat = exp(bsxfun(@plus,Alphamat1,bsxfun(@times,Betamat1,Xmat)))
>
> It didn't really help. Actually it resulted in an increase in time.I tried splitting it up using a temporary variable, but that didn't help either. On profiling, the exp operation is the biggest bottleneck, but I'm hoping vectorizing could reduce the number of calls and thus save time.
>
> Thanks for any suggestions..

one of the solutions
- the for loop most likely is the fastest candidate
- if(f) you take the costly EXP out of it and run the function at the end...

% 1) loop
for i=1:n
     y(:,:,i)=bm.*x(:,:,i);
end
% 2) or
     y=bsxfun(@times,x,bm)
% 3) final pathway...
     y=exp(am+y);

us

Subject: vectorize loop exponential

From: Rajgopal

Date: 17 Aug, 2009 22:54:01

Message: 3 of 3

Thanks a lot for the tip. The bsxfun version is faster after adjusting the dimensions of bm.. The exp outside saves a lot of time too!

"us " <us@neurol.unizh.ch> wrote in message <h6baha$j4f$1@fred.mathworks.com>...
> "Rajgopal " <runraj@gmail.com> wrote in message <h6b5lm$99t$1@fred.mathworks.com>...
> > Hi All
> >
> > Any ways to vectorize this loop better
> >
> > N = 100000
> >
> > for i = 1 to N
> > Ymat(:,:,i) = exp(Alphamat + Betamat.*(Xmat(:,:,i))
> > end
> >
> > All the matrices are 4*4 matrices.
> >
> > I tried..
> >
> > Alphamat1 = repmat(Alphamat,[1 1 N])
> > Betamat1 = repmat(Betamat, [1 1 N])
> > Ymat = exp(bsxfun(@plus,Alphamat1,bsxfun(@times,Betamat1,Xmat)))
> >
> > It didn't really help. Actually it resulted in an increase in time.I tried splitting it up using a temporary variable, but that didn't help either. On profiling, the exp operation is the biggest bottleneck, but I'm hoping vectorizing could reduce the number of calls and thus save time.
> >
> > Thanks for any suggestions..
>
> one of the solutions
> - the for loop most likely is the fastest candidate
> - if(f) you take the costly EXP out of it and run the function at the end...
>
> % 1) loop
> for i=1:n
> y(:,:,i)=bm.*x(:,:,i);
> end
> % 2) or
> y=bsxfun(@times,x,bm)
> % 3) final pathway...
> y=exp(am+y);
>
> us

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
code us 17 Aug, 2009 06:19:03
exp us 17 Aug, 2009 06:19:03
rssFeed for this Thread

Contact us at files@mathworks.com