question of applying sum() to the matrix with zero values

1 visualización (últimos 30 días)
my matrix is X=[1 0 0 ;0 0 0;0 9 0](actual matrix is larger than this and having less values more zoros) my question is ,when applying sum(X),it gives error message "??? Subscript indices must either be real positive integers or logicals."

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 7 de Sept. de 2013
You've probably used sum as a variable in your code. Try this
clear sum
X=[1 0 0 ;0 0 0;0 9 0]
sum(X)
  3 comentarios
Image Analyst
Image Analyst el 7 de Sept. de 2013
Be aware that sum will be over columns and will be a ax3 array. If you want the sum of the whole array, you can do this:
theSum = sum(X(:));
which turns X into a 1D vector before it gets summed. theSum will then be a scalar (single number) rather than a 1 by 3 array.
mahesh chathuranga
mahesh chathuranga el 9 de Sept. de 2013
thank you very much for helping me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by