Capturing non-zeros elements in matrix

Let's say we have a matrix A.From matrix A, I want to remove zeros and create another elements,capturing only non-zeros values. Can you please show me how to perform the above operation.

Respuestas (3)

Jan
Jan el 27 de Feb. de 2011
A = [0 2 3; 4 3 0]
B = A(A ~= 0);
Now B is a vector, not a matrix anymore.

1 comentario

Walter Roberson
Walter Roberson el 28 de Feb. de 2011
Or if conciseness is more important than speed:
B = A(~~A)

Iniciar sesión para comentar.

Andreas Goser
Andreas Goser el 27 de Feb. de 2011
A=[0 2 3; 4 3 0]
find(A)

2 comentarios

Walter Roberson
Walter Roberson el 28 de Feb. de 2011
That would give you the locations of the non-zero elements, but would not in itself create a new array with the non-zero elements. Similar to Jan's answer, this would have to be extended to
B = A(find(A))
Jan
Jan el 28 de Feb. de 2011
@Walter: Of course. I'm sure that Andreas believes in the power of the OP to find this obvious solutionby himself.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 28 de Feb. de 2011
B = nonzeros(A);
The result will be a column vector.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Feb. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by