how to vectorise this for loop?

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 件のコメント

Stephen23
Stephen23 2018 年 8 月 14 日
編集済み: Stephen23 2018 年 8 月 14 日
What are n, M, Jint, alpha and beta? What are the initial values of h1HatN and h1Hat2N? What value does numHarm have?
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN 2018 年 8 月 14 日
n = 262144 x 1 array M = 262144 x 1 array Jint = 263 alpha = 1 x 400 array beta = 1 x 400 array
Initial values of h1HatN and h1Hat2N will be an array of size 262144 x 1
AKHILESH KESAVANUNNITHAN
AKHILESH KESAVANUNNITHAN 2018 年 8 月 14 日
numHarm = 310
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 日

0 投票

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by