How can I detect NaN values in a matrix or vector?

How do I identify NaN values?

1 commentaire

Fawad Chandio
Fawad Chandio le 15 Août 2019
Hello I doing facial recognition attendance system for my final year project I already completed some task but now the problem is that how to recognitions faces...? Help me sir

Connectez-vous pour commenter.

 Réponse acceptée

Doug Hull
Doug Hull le 18 Jan 2011
By definition, NaN is not equal to any number, not even NaN itself. Therefore there are two ways to detect NaN values:
% Generate sample data
x = rand(1, 10);
x(x > 0.5) = NaN;
% Find NaN values in two different ways
y1 = isnan(x) ;
y2 = (x ~= x) ;
For speed purposes the use of isnan() tends to be 20%-30% faster. Here's a test snippet if you want to see the comparison:
A = rand(1000); %Random 1000x1000 matrix
A(rand(size(A))>.75) = nan; %Populate with NaNs
t1 = 0; %time of isnan
t2 = 0; %time of ~=
for ii = 1:100
tic
idx1 = isnan(A);
t1 = t1+toc;
tic
idx2 = A~=A;
t2 = t2 + toc;
end
ratio = t2/t1; %ratio of ~= to isnan
isequal(idx1,idx2) %Insure same results
%{
ratio = 1.2179
ans = 1
%}
[From the MATLAB FAQ of Ancient Times...]

Plus de réponses (1)

Bozydar Wrona
Bozydar Wrona le 22 Juin 2017
Modifié(e) : Bozydar Wrona le 22 Juin 2017

1 vote

%num is a vector/matrix, if searching for elements different than NaN then:
num(isnan(num)==0)
%otherwise:
num(isnan(num)==1)

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by