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

3 Ansichten (letzte 30 Tage)
Doug Hull
Doug Hull am 18 Jan. 2011
Kommentiert: Fawad Chandio am 15 Aug. 2019
How do I identify NaN values?
  1 Kommentar
Fawad Chandio
Fawad Chandio am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

Bozydar Wrona
Bozydar Wrona am 22 Jun. 2017
Bearbeitet: Bozydar Wrona am 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)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by