How to remove the pairing X value when Y is NaN?

1 回表示 (過去 30 日間)
swallow ooi
swallow ooi 2013 年 3 月 13 日
I have a series of XY pairing data where:
X [0 0.5 1 1.5 2 2.5]
Y [2 NaN 7 8 NaN NaN]
I have use this code Z=Y(isfinite(Y)) to remove the NaN in Y.
My question is how can I remove the data for X as well when the pairing Y data is NaN. It means I want to get the results in this way:
X [0 1 1.5]
Y [2 7 8]
Thanks so much~

回答 (3 件)

Friedrich
Friedrich 2013 年 3 月 13 日
Hi,
since X and Y have the same length do:
X = X(isfinite(Y))
Y = Y(isfinite(Y))

Vishal Rane
Vishal Rane 2013 年 3 月 13 日
The simplest way I can think of is :
NotNaN = ~isnan(Y); % gives 1 0 1 1 0 0
Y(NotNaN) % gives 2 7 8
X(NotNaN) % gives 0 1.0000 1.5000

Andrei Bobrov
Andrei Bobrov 2013 年 3 月 13 日
編集済み: Andrei Bobrov 2013 年 3 月 13 日
X = [0 0.5 1 1.5 2 2.5];
Y = [2 NaN 7 8 NaN NaN];
XY = [X(:),Y(:)];
out = XY(~isnan(Y),:); % your case
% the general case
out = XY(all(~isnan(XY),2),:);

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by