필터 지우기
필터 지우기

how to write an if statement for a matrix with some NaN elements?

조회 수: 2 (최근 30일)
Mnr
Mnr 2015년 5월 5일
편집: Stephen23 2015년 5월 6일
Hello all,
I have a matrix with some elements 0s or 1s and the rest are NaNs. I would like to write an if statement that ignores the NaN elements of the matrix. I have tried
if (a(:)==1 or a(:)==0)
------------
end
however, I am getting an error. I would appreciate if somebody can help me please. Thanks.
  댓글 수: 7
Stephen23
Stephen23 2015년 5월 6일
편집: Stephen23 2015년 5월 6일
As Walter Roberson pointed out, it is very important to know that NaN is never equal to anything, not even itself:
>> NaN==NaN
ans =
0
The only way to test for NaN is using isnan, (or by implication isinf and isfinite):
Walter Roberson
Walter Roberson 2015년 5월 6일
~(A==A) is also a test for A being NaN.

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

답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 5일
nonnanlocs = ~isnan(a);
a(nonnanlocs) = SomeFunction(a(nonnanlocs));

카테고리

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