How to use the prod function on non-zero elements?

4 ビュー (過去 30 日間)
Zoltán Csáti
Zoltán Csáti 2014 年 3 月 28 日
コメント済み: Zoltán Csáti 2014 年 3 月 28 日
I have a matrix with zeros in its main diagonal. I would like to take the product of the elements in each row (excluding the 0) so as to get a column vector. How can I make it using a vectorized code? E.g.
A = [0 1 2; 2 0 4; 7 8 0];
I want to get:
[1*2; 2*4; 7*8]
Thank you.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 28 日
編集済み: Azzi Abdelmalek 2014 年 3 月 28 日
A = [0 1 2; 2 0 4; 7 8 0];
B=A
B(~A)=1 % replace 0 elements by 1
out=prod(B,2)
  1 件のコメント
Zoltán Csáti
Zoltán Csáti 2014 年 3 月 28 日
How stupid I am! I should have thought of it. Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by