フィルターのクリア

how to find location of nans in a matrix

17 ビュー (過去 30 日間)
ahmad
ahmad 2023 年 2 月 28 日
コメント済み: Stephen23 2023 年 2 月 28 日
I have a very large matrix. I need to find locations of nans (not a number) in the matrix. for example if there exists a nan in row=2 and coloumn=5, It must give me the answer (2,5)
what do I have to do?

採用された回答

Walter Roberson
Walter Roberson 2023 年 2 月 28 日
[r, c] = find(isnan(YourMatrix)) ;

その他の回答 (1 件)

Luca Ferro
Luca Ferro 2023 年 2 月 28 日
supposing you matrix is named 'm':
nan=isnan(m); %returns 1 where NaN
[rIdx,cIdx]=find(nan==1); %returns row and col indeces whre the 1 is
nanIdx=[rIdx(:),cIdx(:)]; %creates rows column pairs, every row represents a pair
%to access the pairs do:
nanIdx(1,:) %first pair (row-col where NaN is)
nanIdx(2,:) %second pair, and so on
  1 件のコメント
Stephen23
Stephen23 2023 年 2 月 28 日
It would be best not using variable name NAN and shadowing the inbuilt function of that name:
nan(1,2)
ans = 1×2
NaN NaN
Note that nan==1 converts a logical array to an exactly identical logical array, so that EQ is superfluous.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by