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 comentarios

James Tursa
James Tursa el 19 de Sept. de 2017
Please correct the code that you posted. Then let us know the dimensions of the variables involved.
denis bertin
denis bertin el 1 de Oct. de 2017
Hi everyone,
I have these attached file name: datineltempo_ciclico.m that read file name malta1t.mat,
in th line 95, there is the peace of code like this:
NPF = 942;
N=2001;
v=381x1;(complex numbers)
f=381x1;
t=1x2001;
for(k = 1:NPF)
v = datif2(:,k);
g = zeros(2001,1);
du=f(2)-f(1);
for k1=1:N
integrando=exp(i*2*pi*t(k1)*f).*v;
g(k1)=du*sum(integrando);
end
v1 = g;
v1 = 2 * real(v1);
datit(:,k) = v1;
%k
end
.....
The problem is that is take more time to load the 942*2001 matrix;
Please somebody can i help me optimize(make faster) this loop?
Many thanks.
I need the maximum time reduce execution...
Please Help Me....
denis bertin
denis bertin el 3 de Oct. de 2017
Editada: denis bertin el 3 de Oct. de 2017
Hi,
The modification is to put for example 'tic' and 'toc' in the init and end of code to debug the execution time for all the program.
Now it take too many time to execute. just launch a function call: datineltempo_ciclico.m , then the will open a pop up to select the malta1t.mat file, and enter 0.02. and the execution will start. But is too long time take.
Thank you. If you have another problem to execute it,let me know about please.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 1 de Oct. de 2017
Editada: Jan el 1 de Oct. de 2017

0 votos

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 comentarios

denis bertin
denis bertin el 1 de Oct. de 2017
Editada: Walter Roberson el 3 de Oct. de 2017
Hi Simon, is this my interplin function:
I use matlab 2017a version.
Many Thanks you in advance.
function ff=interplin(p1,p2,val1,val2,pm)
m=(1/(p2-p1))*(val2-val1);
n=val1-p1*m;
ff=pm*m+n;
denis bertin
denis bertin el 1 de Oct. de 2017
Hi Simon, I don't know if it was better using ifft/fft function? but i don't know how can use do it better. Thank you for your help.
denis bertin
denis bertin el 2 de Oct. de 2017
Editada: denis bertin el 2 de Oct. de 2017
I attached the "interplin" function...
Jan
Jan el 3 de Oct. de 2017
What should I enter for "dammi il passo di misura desiderato"? I do not have any idea about what the code does, because I do not speak Italian. Therefore I is hard to guess, what modifications are possible. sorry.
denis bertin
denis bertin el 13 de Oct. de 2017
Editada: denis bertin el 13 de Oct. de 2017
Hi JAN, Sorry, i check the two Output:
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
with:
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
but the two datit matrix are differents.
How is it possible please?
denis bertin
denis bertin el 13 de Oct. de 2017
May be is missing sum(...) function on the second code?

Iniciar sesión para comentar.

Más respuestas (1)

denis bertin
denis bertin el 3 de Oct. de 2017
Editada: denis bertin el 3 de Oct. de 2017

0 votos

Hi JAN SIMON, sorry for this missing information. dammi il passo di misura desiderato? enter: 0.02 . it's all.
The modification is to put for example 'tic' and 'toc' in the init and end of code to debug the execution time for all the program.
Now it take too many time to execute. just launch a function call: datineltempo_ciclico.m , then the will open a pop up to select the malta1t.mat file, and enter 0.02. and the execution will start. But is too long time take.
Thank you. If you have another problem to execute it, i can just rename file italian->to English, this is not a problem.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Sept. de 2017

Comentada:

el 13 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by