How to use the prod function on non-zero elements?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zoltán Csáti
am 28 Mär. 2014
Kommentiert: Zoltán Csáti
am 28 Mär. 2014
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 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 28 Mär. 2014
Bearbeitet: Azzi Abdelmalek
am 28 Mär. 2014
A = [0 1 2; 2 0 4; 7 8 0];
B=A
B(~A)=1 % replace 0 elements by 1
out=prod(B,2)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!