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 个)
Image Analyst
2018-1-21
1 个投票
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.
Matt J
2018-1-22
A less efficient way,
z=0:K;
a=exp((-2j*pi/K)*z.'*z)*s(:);
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!