The sum of positive elements of a matrix
Show older comments
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
More Answers (1)
Mohamed Hossam
on 14 Jan 2021
0 votes
is there a certain command that gets the sum of postive even numbers of an array?
1 Comment
Christopher Wallace
on 14 Jan 2021
sum(M((M > 0) & (mod(M, 2) == 0)))
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!