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

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

How stupid I am! I should have thought of it. Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2014년 3월 28일

댓글:

2014년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by