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

3 次查看(过去 30 天)
How do I identify NaN values?
  1 个评论
Fawad Chandio
Fawad Chandio 2019-8-15
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

请先登录,再进行评论。

采纳的回答

Doug Hull
Doug Hull 2011-1-18
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...]

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by