how to vectorise this for loop?

조회 수: 3 (최근 30일)
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN 2018년 8월 14일
댓글: AKHILESH KESAVANUNNITHAN 2018년 8월 14일
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
Jan
Jan 2018년 8월 14일
@AKHILESH KESAVANUNNITHAN: Please post a piece of code, which is running by copy&paste.
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN 2018년 8월 14일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

OCDER
OCDER 2018년 8월 14일
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
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN 2018년 8월 14일
This change improved execution from 7s to 5.6s

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by