I'm trying to vectorize and split up a FOR loop to make it run faster but the variable "aa_sig_combined_vect" begins to return nothing but zeros after cell 5569 any idea how to fix this? see code below that The user krisdestruction helped me with:
Please note I'm using Ubuntu 14.04 with Octave 3.8.1 which is like matlab but is missing some commands unfortunately the parfor command isn't fully implemented in this version of octave.
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);
The original working loop I'm trying to improve the speed of is below
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