How to replace For loops?
Mostrar comentarios más antiguos
I wrote an code with a lots of For loops. I want to replace them and to use functions instead.
wich functions do the same actions?
The code:
w0=(2*pi)/2001;
ak1=zeros(1,2001); %initializing the coeffecients array
%calculating Fourier coeffecients
for k=0:1:2000
s=0;
for n1= 1:1:2001
s=a(n1)*exp(-1i*k*w0*(n1-1001))+s;
end
ak1(k+1)=(1/N)*real(s);
end
Thank's for helping ,
Liron
2 comentarios
dpb
el 13 de Abr. de 2020
Try vectorize on your expressions as starter...
Then just replace the loops with a vector expression for the variable that is in the loop...
Liron Sabatani
el 13 de Abr. de 2020
Respuesta aceptada
Más respuestas (1)
David Hill
el 13 de Abr. de 2020
Editada: David Hill
el 13 de Abr. de 2020
No loops except arrayfun loop.
w0=(2*pi)/2001;
n1=1:2001;
k=0:2000;
s=arrayfun(@(x)sum(a(n1).*exp(-1i*x*w0.*(n1-1001))),k);
ak1=real(s)/N;
1 comentario
Liron Sabatani
el 13 de Abr. de 2020
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!