how to vectorise this for loop?
Show older comments
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 Comments
AKHILESH KESAVANUNNITHAN
on 14 Aug 2018
AKHILESH KESAVANUNNITHAN
on 14 Aug 2018
Jan
on 14 Aug 2018
@AKHILESH KESAVANUNNITHAN: Please post a piece of code, which is running by copy&paste.
AKHILESH KESAVANUNNITHAN
on 14 Aug 2018
Answers (1)
OCDER
on 14 Aug 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 Comment
AKHILESH KESAVANUNNITHAN
on 14 Aug 2018
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!