I have a function with the following for-loop that seems to be pretty efficient. However, I'm wondering if this is the most efficient implementation, or is there a faster way to do this computation. There is a helper function too "Pnm_index" that finds the proper index to store the computations in the final output variable Pnms. I've included that function as well below. Since this helper function is so simple I'm also wondering if eliminating it will provide any speed up. Any insight is appreciated!
Pnms = zeros(1,((p+1)*(p+2))*(2^-1));
Pnm = c * (2 * m + 1) * tmp1;
Pnm = (c * (2 * n + 1) * tmp1 - (n + m) * tmp2) / (n - m + 1);
toc
Elapsed time is 0.042007 seconds.
function out = Pnm_index(n,m)
out = (n*(n+1))*(2^-1) + m + 1;