Finding columns that contain both integers and NaN

1 回表示 (過去 30 日間)
Ahmed Hamed
Ahmed Hamed 2016 年 2 月 4 日
コメント済み: the cyclist 2016 年 2 月 4 日
I have a matrix that contain Numerical values (float) and Categorical values (Int)
as
A = 1 NaN 2.2 3.2 4
NaN 7.9 5.1 NaN 5
3 5.5 NaN 4.1 NaN
and I'd like to split it into matrices;
one contains the Int values and the other contains the float values as
it = 1 4
NaN 5
3 NaN
and
flt = NaN 2.2 3.2
7.9 5.1 NaN
5.5 NaN 4.1
i use the following code
int_cols = all( round(A)==A, 1 ); %// logical indexing into integer columns
it = A(:,int_cols);
flt = A(:,~int_cols);
However, NaN isn't equal NaN so it appears in the flt matrix and it = [].
Are there any function that can help?

回答 (2 件)

David Young
David Young 2016 年 2 月 4 日
Yes, isnan(x) does what x == NaN would do if NaNs were equal to each other.

the cyclist
the cyclist 2016 年 2 月 4 日
int_cols = all( round(A)==A | isnan(A), 1 ); %// logical indexing into integer columns
it = A(:,int_cols)
flt = A(:,~int_cols)
  1 件のコメント
the cyclist
the cyclist 2016 年 2 月 4 日
If the entries in A are have been computed in prior operations, you might also want to guard against floating point inaccuracies, such as the value "4" actually being 4.0000000000001. You could check for equality to within a specific tolerance, for example.

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

カテゴリ

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