How can I remove all rows from a matrix which contain NaN values?
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 14 Nov. 2024
Bearbeitet: MathWorks Support Team
am 29 Jan. 2025 um 13:43
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].
Akzeptierte Antwort
MathWorks Support Team
am 29 Jan. 2025 um 0:00
Bearbeitet: MathWorks Support Team
am 29 Jan. 2025 um 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 Kommentar
Walter Roberson
am 14 Nov. 2024
A = [1, 2, 3; 4, NaN, 6; 7, 8, 9];
A = rmmissing(A)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Numeric Types finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!