|
From: | RT |
Subject: | Splitting up FOR loop for vectorizing so it runs quicker coding issue |
Date: | Thu, 25 Jun 2015 01:31:58 -0400 |
t=rand(1,556790);
inner_freq=rand(8193,6);
N=100; % use N chunks
nn = int32( linspace(1, length(t)+1, N+1) );
aa_sig_combined_vect=zeros(size(t));
total_time_so_far=0;
D = diag(inner_freq(1:end-1,2));
A = inner_freq(1:end-1,1);
for ii=1:N
ind = nn(ii):nn(ii+1)-1;
tic;
cosPara = 2 * pi * A * t(ind);
toc;
cosResult = cos( cosPara );
sumParaA = D * cosResult;
toc;
sumParaB = repmat(inner_freq(1:end-1,3),[1 length(ind)]);
toc;
aa_sig_combined_vect(ind) = sum( sumParaA + sumParaB );
toc;
total_time_so_far=total_time_so_far+sum(toc)
return;
end
fprintf('- Complete test in %4.4fsec or %4.4fmins\n',total_time_so_far,total_time_so_far/60);
clear all,
t=rand(1,556790);
inner_freq=rand(8193,6);
N=100; # use N chunks
nn = int32(linspace(1, length(t)+1, N+1))
aa_sig_combined=zeros(size(t));
total_time_so_far=0;
for ii=1:N
tic;
ind = nn(ii):nn(ii+1)-1;
aa_sig_combined(ind) = sum(diag(inner_freq(1:end-1,2)) * cos(2 .* pi .* inner_freq(1:end-1,1) * t(ind)) .+ repmat(inner_freq(1:end-1,3),[1 length(ind)]));
toc
total_time_so_far=total_time_so_far+sum(toc)
end
fprintf('- Complete test in %4.4fsec or %4.4fmins\n',total_time_so_far,total_time_so_far/60);
RMSERepmat = sqrt(mean((aa_sig_combined-aa_sig_combined_vect).^2)) %root men square error between two arrays lower is better
[Prev in Thread] | Current Thread | [Next in Thread] |