MATLAB: How to efficiently compute a sum of large vectors with elements depend on the sum index

sumvectorization

I have to compute the sum: , where are large column vectors ( dimension is about 10 millions) and κ is about 100 to 200.
My first attempt is , with (a row vector) and denotes the element-wise product.
My second attempt is to use for loop.
Both attempt are still very slow but for loop is slightly faster.
My questions is : Is there any way to compute the sum more efficiently?
Any suggestion would be much appreciated !

Best Answer

%assume that z and y are COLUMN vectors
K = (1:kappa).'; %COLUMN vector not row vector
result = sum( K.*sin(K.*z.').*exp(K.*y.') );
What this does is improve locality of reference. The values to be summed will be consecutive ones in the matrix, instead of being scattered around the matrix.