Simple Method for Finding if ANY NaN values occur in a matrix.

조회 수: 425 (최근 30일)
BM
BM 2018년 3월 20일
답변: Stefan Siemens 2018년 11월 12일
Is there a quick method of finding out whether or not a matrix or a vector in matlab has any NaN values? A large matrix filled with mostly 0's (after applying isnan(MATRIX)) does not really cut it.
Don't get me wrong, I would information of finding out where such NaN's occur and how to count how many NaN values appear in a given matrix, but I really would like to know if there is a simple way to identify if any NaN's at all appear in a given matrix with a simple 1 or 0 response, as it would save me time having to inspect the matrix or write a program to do it.
Would there be simple ways of counting how many NaN's occur, and identify their location in a given matrix? I assume such techniques would be applicable to finding infinities as well, am I correct?

채택된 답변

James Tursa
James Tursa 2018년 3월 20일
편집: James Tursa 2018년 3월 20일
Short of a mex routine that can avoid the creation of a potentially large intermediate array, I think you are stuck with isnan( ). E.g., counting the number of NaN's
result = sum(isnan(MATRIX(:)));
Or if you want locations, then find( ) etc.
And yes, you can do similar calculations with the isinf( ) function.
A mex routine to do this would not be too difficult to write, btw ...

추가 답변 (3개)

Stefan Siemens
Stefan Siemens 2018년 11월 12일
Personally I think
any(isnan(your_matrix(:)));
is the easiest way.

Jan
Jan 2018년 3월 21일
Here is a fast C-Mex function to find out, if any element of one array occurs in the other: FEX: anyEq .
hasAnyNaN = anyEq(X, NaN);
This does not create a temporary array like isnan(X) and stops the search at the first match already. So this is treated as efficient as possible:
X = NaN(1, 1e6);
T = anyEq(X, NaN); % Performs 1 comparison only

BM
BM 2018년 3월 20일
Too bad! I was hoping I could get away with working in the command prompt rather than write a routine directly in the program, as it is complicated enough already.
I forgot about the built-in 'sum' command, but your listing just gave me an idea. If the sum is 0, then no NaN values can be in the matrix. If it is anything other than 0, then at least one NaN exists in the matrix. Your suggestion seems to have lead me to figuring out how to quickly decipher if any NaN values exist in the matrix! All I would need to do is check if the final result is equal to 0 after the summation! Thanks for jump-starting my thinking!
  댓글 수: 18
BM
BM 2018년 3월 21일
편집: BM 2018년 3월 21일
And if that's the case, they can simply define the function f = function_name
as you showed above
I just didn't want to add another variable to the list I already had. Because I was troubleshooting in this particular way, it did me no good to do so either.
Walter Roberson
Walter Roberson 2018년 3월 21일
"I just didn't want to add another variable to the list I already had"
disp(nanappear(n))
If that is too much, then, Sure, you can use
function nanappear(n)
disp(any(isnan(n(:))));
But I would not expect that other people would find it useful.
Note: I might suggest "anynan" as the function name.

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

카테고리

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