Simple Method for Finding if ANY NaN values occur in a matrix.
180 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
BM
am 20 Mär. 2018
Verschoben: Dyuman Joshi
am 7 Mai 2024
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?
0 Kommentare
Akzeptierte Antwort
James Tursa
am 20 Mär. 2018
Bearbeitet: James Tursa
am 20 Mär. 2018
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 ...
19 Kommentare
Walter Roberson
am 21 Mär. 2018
Verschoben: Dyuman Joshi
am 7 Mai 2024
"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.
Weitere Antworten (3)
Stefan Siemens
am 12 Nov. 2018
Personally I think
any(isnan(your_matrix(:)));
is the easiest way.
0 Kommentare
Jan
am 21 Mär. 2018
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
Charles Rice
am 7 Mai 2024
If you're finding this answer post 2022, there's a new function anynan(A).
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!