In my program I need to calculate the sum:
.
I calculate this sum 2500 times with new values of C
and z
.
Argument z may be a vector. I wrote straightforward for-loop and vectorized version code as follows:
K = 200;
n_z = 40000;
C = ones(K,1); % an example, in real life the arey some coefficients (at next call will be new)
k = 0:K-1;
z = linspace(0, 2*pi, n_z); % at next call will be new
tic;
my_sum_for = zeros(1, K);
for i=1:n_z
my_sum_for(i) = C' * tan(k' * z(i));
end
toc; % Elapsed time is 1.820485 seconds.
tic;
my_sum = C' * tan(k' * z);
toc; % Elapsed time is 0.160924 seconds.
Vectorized version is faster, but not enough. Is it possible to improve vectorized version?
After Dominique Jacquel's answer I have this vectorized version, it's faster:
K = 200;
n_z = 40000;
C = ones(K,1)'; % an example, in real life they are some coefficients (at next call will be new)
k = (0:K-1)';
z = linspace(0, 2*pi, n_z); % at next call will be new
tic;
my_sum_for = zeros(1, K);
for i=1:n_z
my_sum_for(i) = C * tan(k * z(i));
end
toc; % Elapsed time is 1.521587 seconds.
tic;
my_sum = C * tan(k * z);
toc; % Elapsed time is 0.125468 seconds.
Is it possible to improve vectorized version even more (bsxfun, arrayfun or something)? The time of 250 seconds is still slow for me (it is 75% of all compuations).