フィルターのクリア

Dropping NaN observations from Matrix

1 回表示 (過去 30 日間)
Edward Johnsen
Edward Johnsen 2019 年 1 月 23 日
コメント済み: Kevin Phung 2019 年 1 月 23 日
Hey There.
So i have a matrix that has a person ID for the first column, then data in the rows for each. What i need to do is i have identified that there are some observations that are NaN, so i need to write a code that identifies which person ID have NaN observations, so that i can drop those observations from the data.
Any help could be appreciated

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 1 月 23 日

Kevin Phung
Kevin Phung 2019 年 1 月 23 日
編集済み: Kevin Phung 2019 年 1 月 23 日
if you had a matrix A and wanted to search for NaNs in each row
nanRows =[];
for i = 1:size(A,1)
if any(isnan(A(i,:))) % for each row, if any of the elements contain a NaN
nanRows(i) = i;% store that index
end
end
A(nanRows,:) = []; %remove those IDs from your matrix
if your matrix is of cell arrays, then replace parentheses with {}.
edit: before you run A(nanRows,:) =[] , you can check which IDs had Nans with just A(nanRows,1)
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 23 日
No loop needed.
A(any(isnan(A),2),:) = [];
Kevin Phung
Kevin Phung 2019 年 1 月 23 日
never noticed any() can take two arguments, that's neat! Thanks Walter.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by