Given the matrix:
A = [1 2 3; 4 5 6; 7 8 9];
For very large matrices using sum(sum(A)) can be faster than sum(A(:)):
sum(sum(A))
sum(A(:))
>> A = rand(20000); >> tic; B=sum(A(:)); toc; tic; C=sum(sum(A)); toc Elapsed time is 0.407980 seconds. Elapsed time is 0.322624 seconds.