I was wondering whether there is a faster way to evaluate this double sum in matlab:
$$\sum_{n=1}^{\text{max}} \sum_{m=-n}^{n} f(n,m).$$ Cause I am currently doing this with a foor loop over n and m and noticed that my code is tremendously slow. ( I know that matlab is faster with vectorized code, but I do not really know what actually the best alternative is)
You can try to generate a static $2\times N$ array with
$$N = \sum_{n=1}^{\text{max}_n} 2n + 1 = \text{max}_n + \text{max}_n(\text{max}_n + 1) = \text{max}_n^2 + 2\text{max}_n$$ Note $(n-1)^2 + 2(n-1) = n^2 - 1$
MN = zeros(2,maxn^2 + 2*maxn);for n=1:maxn
tmp = n*ones(2,2*n + 1);
tmp(2,:) = -n:n;
R = (0:2*n) + n^2;
MN(:,R) = tmp;
end
not tested, as I have no access to MATLAB right now.Then, using save / load you use
sum(f(MN))for the summation.EDIT: The line with
R = ...was bugged. I've fixed it; this way it seems to work. Note, that $f$ must take a $2\times1$ (column)-Vector as input per point.