The sum of positive elements of a matrix

7 ビュー (過去 30 日間)
merowing3
merowing3 2018 年 9 月 18 日
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?

採用された回答

Christopher Wallace
Christopher Wallace 2018 年 9 月 18 日
sum(sum(M(M > 0)))

その他の回答 (1 件)

Mohamed Hossam
Mohamed Hossam 2021 年 1 月 14 日
is there a certain command that gets the sum of postive even numbers of an array?
  1 件のコメント
Christopher Wallace
Christopher Wallace 2021 年 1 月 14 日
sum(M((M > 0) & (mod(M, 2) == 0)))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by