The sum of positive elements of a matrix

11 views (last 30 days)
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?

Accepted Answer

Christopher Wallace
Christopher Wallace on 18 Sep 2018
sum(sum(M(M > 0)))

More Answers (1)

Mohamed Hossam
Mohamed Hossam on 14 Jan 2021
is there a certain command that gets the sum of postive even numbers of an array?

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!