how to vectorise this for loop?

3 visualizaciones (últimos 30 días)
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN el 14 de Ag. de 2018
Comentada: AKHILESH KESAVANUNNITHAN el 14 de Ag. de 2018
for i = 2:numHarm
h1HatN = h1HatN + alpha(i-1)*sin(i*2*pi*Jint*n/M) + beta(i-1)*cos(i*2*pi*Jint*n/M);
h1Hat2N = h1Hat2N + alpha(i-1)*sin(i*2*pi*Jnew2*n/M) + beta(i-1)*cos(i*2*pi*Jnew2*n/M);
end
  5 comentarios
Jan
Jan el 14 de Ag. de 2018
@AKHILESH KESAVANUNNITHAN: Please post a piece of code, which is running by copy&paste.
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN el 14 de Ag. de 2018
alpha = rand(1, numHarm ); beta = rand(1, numHarm ); % h1HatN = rand(z, 1 ); % h1Hat2N = rand(z, 1 );
alpha = zeros(1,numHarm);
beta = zeros(1,numHarm);
for i = 2:numHarm
k = round(i*Jnew2);
cons1 = sin(pi*(i*Jnew2-k))/sin(pi*(i*Jnew2-k)/M);
cons2 = sin(pi*(i*Jnew2+k))/sin(pi*(i*Jnew2+k)/M);
theta1 = pi*(M-1)*(i*Jnew2-k)/M;
theta2 = pi*(M-1)*(i*Jnew2+k)/M;
cons3 = ((sin(pi*(Jnew2-k))/sin(pi*(Jnew2-k)/M))*(exp((1i)*(a*(Jnew2-k)+phi))) + (sin(pi*(Jnew2+k))/sin(pi*(Jnew2+k)/M))*(exp(-(1i)*(a*(Jnew2+k)+phi))))*(A/(2*M));
Y = [cons1*sin(theta1)+cons2*sin(theta2) cons1*cos(theta1)+cons2*cos(theta2); cons2*cos(theta2)-cons1*cos(theta1) cons1*sin(theta1)-cons2*sin(theta2)];
Z = [2*M*real((yfft(mod(round(i*Jnew2),M)+1)-cons3)) 2*M*imag((yfft(mod(round(i*Jnew2),M)+1)-cons3))]';
% Y inversion start
detY = (Y(1,1)*Y(2,2)-Y(1,2)*Y(2,1));
invY = [Y(2,2) -Y(1,2); -Y(2,1) Y(1,1)]/detY;
% Y inversion end
SecParams = (invY)*Z;
alpha(i-1) = SecParams(1);
beta(i-1) = SecParams(2);
end
% ----- Construct the non-coherent and coherent fundamental -----
n=ones(size(dataRec));
n=cumsum(n)-1;
h1HatN = Amp*cos((2*pi()*Jint*n/M)+phi); % Coherent fundamental
h1Hat2N = Amp*cos((2*pi()*Jnew2*n/M)+phi); % Non-coherent fundamental
dataProc2Ne = dataRec - h1Hat2N; % Error containing the information of harmonics and noise.
% ----- Add the non-coherent harmonics to the non-coherent fundamental
% and coherent harmonics to the coherent fundamental -------------
for i = 2:numHarm
h1HatN = h1HatN + alpha(i-1)*sin(i*2*pi*Jint*n/M) + beta(i-1)*cos(i*2*pi*Jint*n/M);
h1Hat2N = h1Hat2N + alpha(i-1)*sin(i*2*pi*Jnew2*n/M) + beta(i-1)*cos(i*2*pi*Jnew2*n/M);
end

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 14 de Ag. de 2018
Jint = 263;
Jnew2 = 360;
z = 262144;
numHarm = 400;
n = rand(z, 1);
M = rand(z, 1);
alpha = rand(1, numHarm);
beta = rand(1, numHarm);
h1HatN = rand(z, 1);
h1Hat2N = rand(z, 1);
i = [2:numHarm];
h1HatN = h1HatN + sum(alpha(i-1).*sin(i*2*pi*Jint.*n./M) + beta(i-1).*cos(i*2*pi*Jint.*n./M), 2);
h1Hat2N = h1Hat2N + sum(alpha(i-1).*sin(i*2*pi*Jnew2.*n./M) + beta(i-1).*cos(i*2*pi*Jnew2.*n./M), 2);
%check this n/M vs n./M . n/M for 262144x1 / 262144x1 matrix will generate a 512 GB matrix!
  1 comentario
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN el 14 de Ag. de 2018
This change improved execution from 7s to 5.6s

Iniciar sesión para comentar.

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by