For loop Optimization?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
denis bertin
am 19 Sep. 2017
Kommentiert: denis bertin
am 13 Okt. 2017
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 Kommentare
Akzeptierte Antwort
Jan
am 1 Okt. 2017
Bearbeitet: Jan
am 1 Okt. 2017
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 Kommentare
Weitere Antworten (1)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!