MATLAB: The sum of positive elements of a matrix

matrixsum

Let M be a matrix with dimensions mxn. Calculate the sum of all positive elements of matrix M without using loop.
My code:
sum(M>0) % sum of all positive elements in respective column, we get vector with sum for each column
sum(sum(M>0)) % sum of all sums from vector
Correct or not?

Best Answer

sum(sum(M(M > 0)))