MATLAB: Matrix multiplication: exponentiation before summation

matrix multiplicationtensor

Normal matrix multiplication would give C=A*B, such that C_ij = sum_k { A_ik * B_kj }
I want to compute a C* such that C*_ij = sum_k { exp( A_ik * B_kj ) }
That is, I want to exponentiate all the results of multiplication before they are summed into a single cell. How should I code this in matlab?

Best Answer

Thanks for your help John, Azzi, and Image Analyst.
John's answer is very thorough indeed. Here's something I wrote that's reasonably readable, equal in speed as C3, and avoids memory blowup completely.
C4=zeros(500,400);
for j=1:400
C4(:,j) = sum(exp(bsxfun(@times, A, B(:,j)')),2);
end
I'm only curious as to how to make it a bit faster. Exponentiating (500*400*1000) elements and doing a normal matrix multiplication (500,1000) with (1000, 400) only takes half as much time as C3 or C4. And exponentiation is certainly the most expensive part here.