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