필터 지우기
필터 지우기

How can I delete the rows if they have a NaN value?

조회 수: 2 (최근 30일)
Emre Eryigit
Emre Eryigit 2021년 4월 15일
댓글: Khalid Mahmood 2021년 4월 15일
Hello all,
My problem is kinda cheezy but I still can't solve the problem so here it is;
So my codes I wrote so far are;
load carsmall
x=[MPG]
y=[Weight]
and I'd likte to delete the rows which contains NaN in my x value. Long story in short I'd like to delete the rows 11,12,13,14,15 and 18 since they have NaN values.
Any helps are appreciated,
Regards,

채택된 답변

Matt J
Matt J 2021년 4월 15일
keep=~isnan(x);
x=x(keep);
y=y(keep);
  댓글 수: 4
Emre Eryigit
Emre Eryigit 2021년 4월 15일
Okay now it's solved thanks a lot! :)
Matt J
Matt J 2021년 4월 15일
You're welcome, but please Accept-click the answer to indicate that it addressed your question.

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

추가 답변 (1개)

Khalid Mahmood
Khalid Mahmood 2021년 4월 15일
편집: Khalid Mahmood 2021년 4월 15일
Let Matrix A has NaN values, we want a matrix B which contains all rows of A except which have NaN values
function testNaNRemoval()
A = [ 1 5 8 ; -3 NaN 14;0 6 NaN ; 1 5 18];
B=remove_nan_rows(A)
end
function B = remove_nan_rows(A)
B=[];ib=1;
for i=1:size(A,1)
if any(isnan(A(i,:))), continue;end
B(ib,:)=A(i,:); ib=ib+1;
end
end
  댓글 수: 1
Khalid Mahmood
Khalid Mahmood 2021년 4월 15일
This is a bit lengthy answer, but keep=~isnan(A); B=A(keep) also works.
Advantage of longer method is when we want mixed behaviour or our tailored processing

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by