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

3 ビュー (過去 30 日間)
Doug Hull
Doug Hull 2011 年 1 月 18 日
コメント済み: Fawad Chandio 2019 年 8 月 15 日
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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by