MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Eli on 21 Jun 2013

Hi, would someone happen to know how I could vectorize the following loop?

L=4; for t=1:N; A(:,:,t)=exp(a*(t-1)).*B(:,:,L); end

many thanks, Eli

Answer by Andrei Bobrov on 21 Jun 2013

Accepted answer

B1 = B(:,:,4); out = reshape(B1(:)*(0:N-1),[size(B1) N]);

Eli on 21 Jun 2013

thanks! this works, but didn't turn out to be more efficient than the loop, I think. here is my try, did I get this right?

clear all; N=1000; a=rand(1,1000); B=rand(128,256,N); C=NaN(128,256,N);

tic for t=1:N C(:,:,t)=B(:,:,4)*a(t); end toc

tic B1 = B(:,:,4); C1 = reshape(B1(:)*a,[size(B1) N]); toc

difference=max(max(max(abs(C1-C))))

output:

Elapsed time is 0.097027 seconds. Elapsed time is 0.110844 seconds.

difference =

0

James Tursa on 22 Jun 2013

Your timing is comparing apples to oranges. You preallocate the answer for your loop but do not include it in your timing, whereas you penalize the reshaped outer product by including its intrinsic allocation in the timing. Your loop timing should be this instead, which includes both the allocation and the operations:

tic C=NaN(128,256,N); for t=1:N C(:,:,t)=B(:,:,4)*a(t); end toc

Also, the timings should be run several times to make sure background functions are loaded into memory (I assume you do not want the function loading times to bias your results). So don't clear all between timing runs, just clear the variables you allocate (e.g., C, B1, and C1).

Eli on 22 Jun 2013

Brilliant of you. the vectorized version seems significantly faster now. thanks! here it is:

%clear all; N=1000; a=rand(1,N); B=rand(128,256,N);

tic C=NaN(128,256,N); for t=1:N C(:,:,t)=B(:,:,4)*a(t); end toc

tic C1 = reshape(reshape(B(:,:,4),[128*256*1,1])*a,[128 256 N]); toc

difference=max(max(max(abs(C1-C))))

output:

Elapsed time is 0.230418 seconds.

Elapsed time is 0.123938 seconds.

difference = 0

Answer by Iain on 21 Jun 2013

Edited by Iain on 21 Jun 2013

A = bsxfun(@times,exp(bsxfun(@times,a,0:(t-1)),B);

You need to make sure that a, 0:t-1 and B are all in the correct dimensions, and that "times", is the correct multiplication for your needs.

A = bsxfun(@times,exp(bsxfun(@times,a,reshape(0:(t-1),1, 1,[]),B);

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/79797#comment_156150

Is "a" a scalar?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/79797#comment_156156

yes