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

조회 수: 20 (최근 30일)
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].

채택된 답변

MathWorks Support Team
MathWorks Support Team 2025년 1월 29일 0:00
편집: MathWorks Support Team 2025년 1월 29일 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
Walter Roberson
Walter Roberson 2024년 11월 14일
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>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by