How to use the prod function on non-zero elements?
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!