How to remove NaN's from an array AND the corresponding value/element in another array of the same size?

4 ビュー (過去 30 日間)
I have 2 arrays: A = [2 3 5 7 9] and B = [1 3 nan 1 nan]
I want to plot(A,B) but when i do that, i will just get data points and no line connecting each point bc there's NaN's between points
(i want lines between points bc i intend to show multiple data sets in this plot eventually)
so i want to remove the nan's in B and the corresponding elements in A (5 and 9)
to remove the nan's in B, i do:
y1 = B(~isnan(B));
so now, y1 = [1 3 1]
how do I make an x1 = [2 3 7]? so i can just plot (x1,y1)
I'm doing this many many times with a large set of data.

採用された回答

Alex Mcaulley
Alex Mcaulley 2020 年 3 月 12 日
x1 = A(~isnan(B));
y1 = B(~isnan(B));
  2 件のコメント
dpb
dpb 2020 年 3 月 12 日
Perhaps easier to visualize (and might be a little faster at expense of a temporary variable) ...
isOK=isfinite(B);
plot(A(isOK),B(isOK))

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by