For loop Optimization?
2 次查看(过去 30 天)
显示 更早的评论
Hi everybody, i want to optmize(make faster loop) this function:
for k=1:2001
integrando=exp(i*2*pi*var_spaziale(k)*var_spettrale).*funzionefor k=1:N
integrando=exp(i*2*pi*var_spaziale(k)*var_spettrale).*funzione;
f(k)=du*sum(integrando);
end
How is it possible to do please?
Many thanks.
3 个评论
采纳的回答
Jan
2017-10-1
编辑:Jan
2017-10-1
The exp() function is expensive. So start with avoiding repeated calls with the same argument:
C = exp((2i * pi * t) .* f); % Auto-expanding, >= R2016b
for k = 1:NPF
v = datif2(:,k);
g = zeros(2001,1);
du = f(2)-f(1);
for k1 = 1:N
integrando = C(:, k1) .* v;
g(k1) = du * sum(integrando);
end
datit(:, k) = 2 * real(g);
end
Is datit pre-allocated before the loop? If not, add this.
For Matlab < 2016b:
C = exp(2i * pi * bsxfun(@times, t, f));
Try if this is faster:
C = exp((2i * pi) * t.' .* f.'); % Auto-expaning, >= R2016b
for k = 1:NPF
du = f(2)-f(1);
g = du * C * datif2(:,k);
datit(:, k) = 2 * real(g);
end
Unfortunately I cannot try this by my own, because your posted code misses the function "interplin". What is it?
6 个评论
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!