How can I remove all rows from a matrix which contain NaN values?

20 visualizaciones (últimos 30 días)
How can I remove all rows from a matrix which contain NaN values?
For example:
>> A = [1, 2, 3; 4, NaN, 6; 7, 8, 9] A = 1 2 3 4 NaN 6 7 8 9
In matrix A defined above, I would like to remove the second row, [4, NaN, 6].

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 29 de En. de 2025 a las 0:00
Editada: MathWorks Support Team el 29 de En. de 2025 a las 13:43
Use the following code to remove all rows which contain NaN values from a matrix A:
>> A = [1, 2, 3; 4, NaN, 6; 7, 8, 9];
>> A = A(~any(isnan(A), 2), :)
A =
1 2 3
7 8 9
Alternatively, make use of the function "rmmissing" to remove any row that contains missing data, as shown below.
>> A = [1, 2, 3; 4, NaN, 6; 7, 8, 9];
>> A = rmmissing(A)
A =
1 2 3
7 8 9
  1 comentario
Walter Roberson
Walter Roberson el 14 de Nov. de 2024
A = [1, 2, 3; 4, NaN, 6; 7, 8, 9];
A = rmmissing(A)
A = 2×3
1 2 3 7 8 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by