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

41 views (last 30 days)
How do I identify NaN values?
  1 Comment
Fawad Chandio
Fawad Chandio on 15 Aug 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

Sign in to comment.

Accepted Answer

Doug Hull
Doug Hull on 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...]

More Answers (1)

Bozydar Wrona
Bozydar Wrona on 22 Jun 2017
Edited: Bozydar Wrona on 22 Jun 2017
%num is a vector/matrix, if searching for elements different than NaN then:
num(isnan(num)==0)
%otherwise:
num(isnan(num)==1)

Tags

Products

Community Treasure Hunt

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

Start Hunting!