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

1 visualización (últimos 30 días)
Zoltán Csáti
Zoltán Csáti el 28 de Mzo. de 2014
Comentada: Zoltán Csáti el 28 de Mzo. de 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.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 28 de Mzo. de 2014
Editada: Azzi Abdelmalek el 28 de Mzo. de 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)
  1 comentario
Zoltán Csáti
Zoltán Csáti el 28 de Mzo. de 2014
How stupid I am! I should have thought of it. Thank you very much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by