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
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
Liron Sabatani el 13 de Abr. de 2020
thank you very much about your answer!
I tried to vectorize but I don't succeed to get rid from all the for loops..
That's what I did:
for k=0:1:2000
n1= (1:1:2001);
s=sum(a(n1).*exp(-1i*k*w0*(n1-1001)));
ak1(k+1)=(1/N)*real(s);
end

Iniciar sesión para comentar.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 13 de Abr. de 2020
Editada: Ameer Hamza el 13 de Abr. de 2020

2 votos

No functions are needed to replace the for loop in your current code. Just matrix operations are enough.
rng(0);
w0=(2*pi)/2001;
a = rand(1,2001); % missing from your posted code
N = 2001;
n1= 1:1:2001;
k=(0:1:2000)';
s = a*exp(-1i*k*w0*(n1-1001)).';
ak1=(1/N)*real(s);
Test if the output is correct
% your code
rng(0);
w0=(2*pi)/2001;
ak1=zeros(1,2001); %initializing the coeffecients array
a = rand(1,2001); % missing from your posted code
N = 2001;
%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
% my code
s = a*exp(-1i*k*w0*(n1-1001)).';
ak2=(1/N)*real(s); % variable name changed to ak2 for comparison
Result:
>> isequal(ak1,ak2)
ans =
logical
1

5 comentarios

Liron Sabatani
Liron Sabatani el 13 de Abr. de 2020
You helped a lot me thank you!!!
Ameer Hamza
Ameer Hamza el 14 de Abr. de 2020
Glad to be of help.
Liron Sabatani
Liron Sabatani el 14 de Abr. de 2020
I tried to do the same with another part of the code but I don't succeed to do so .. please if you can help:)
k=0:1:2000;
ck(k+1)=ak1(k+1).*(1-exp(-1i.*k*w0)); %calculating ck Fourier coeffecients
c=zeros(1,2001); %initializing the array
for n3=1:1:2001 %calculating c signal
k=0:1:2000;
s4=sum(ck(k+1).*exp(1i.*k*w0*(n3-1001)));
c(n3)=real(s4);
end
I really appreciate the help:)
Ameer Hamza
Ameer Hamza el 14 de Abr. de 2020
Following code vectorize it
rng(0);
w0=(2*pi)/2001;
a = rand(1,2001); % missing from your posted code
N = 2001;
n1= 1:1:2001;
k=(0:1:2000)';
s = a*exp(-1i*k*w0*(n1-1001)).';
ak1=(1/N)*real(s);
k=0:1:2000;
ck=ak1.*(1-exp(-1i.*k*w0)); %calculating ck Fourier coeffecients
n3=(1:1:2001)';
k=(0:1:2000);
s4=ck*exp(1i.*w0*(n3-1001)*k).';
c(n3)=real(s4);
Liron Sabatani
Liron Sabatani el 14 de Abr. de 2020
again thank you!! I got the way to do this:)

Iniciar sesión para comentar.

Más respuestas (1)

David Hill
David Hill el 13 de Abr. de 2020
Editada: David Hill el 13 de Abr. de 2020

1 voto

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;

Categorías

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

Preguntada:

el 13 de Abr. de 2020

Comentada:

el 14 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by