MATLAB Answers

0

The sum of positive elements of a matrix

Asked by merowing3 on 18 Sep 2018
Latest activity Answered by Christopher Wallace on 18 Sep 2018
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?

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Christopher Wallace on 18 Sep 2018
 Accepted Answer

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

  0 Comments

Sign in to comment.