Given the matrix:
A = [1 2 3; 4 5 6; 7 8 9];
Avoid for loops whenever possible.
sum(A(:))
is great however if you have some logical indexing going on you can't use the (:) but you can write
% Sum all elements under 45 in the matrix
sum ( sum ( A *. ( A < 45 ) )
Since sum sums the columns and sums the row vector that was created by the first sum. Note that this only works if the matrix is 2-dim.