how can i vectorize this loop?

K=100;
a=zeros(K,1);
for u = 0:K-1
for b = 0:K-1
a(u+1) = a(u+1) + s(b+1)*exp((-2j*pi*u*b/K));
end
end

2 Comments

What is "k" in s(k+1)? DO you mean s(b+1)?
Yes is b+1

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 21 Jan 2018
Well you could try meshgrid() but I think it might make it a little more confusing to understand what it's doing. With only 100x100 array to fill, a double for loop will be very fast so even if you did vectorize it, don't expect to notice any speed difference.
A less efficient way,
z=0:K;
a=exp((-2j*pi/K)*z.'*z)*s(:);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 21 Jan 2018

Answered:

on 22 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!