MATLAB: Summation and array indexes

array indexmultidimensionssummation

I am trying to write a function for an optimisation routine that requires the (total) summation of a matrix divided element by element by a second matrix, where the second matrix has a third dimension returning a vector the size of that third dimension.
i.e. something along the lines of a(1:n) = sum(sum(A./B(:,:,1:n))
which doesn't work. Any ideas?
Thanks

Best Answer

Use bsxfun to perform your division
a = squeeze(sum(sum(bsxfun(@rdivide, A, B))));