How to Ignore NaN values when plotting?

166 ビュー (過去 30 日間)
Saúl Francisco González González
Saúl Francisco González González 2019 年 7 月 27 日
回答済み: Juan Carlos Ortega 2023 年 6 月 14 日
Hi, If anyone can tell me how to ignore NaN values when you are plotting? Thanks
  2 件のコメント
Rik
Rik 2019 年 7 月 27 日
What do you mean? Most plotting functions will already ignore NaN values by default.
dpb
dpb 2019 年 7 月 27 日
+1
Altho there are two ways to consider what the Q? might mean and how the plot might look...consider the following:
y=[1:3 nan 4:7]; % data array w/ an included NaN
subplot(2,1,1)
plot(y,'*-')
legend('NaN silently ignored','Location','northwest')
subplot(2,1,2)
plot(y(isfinite(y)),'*-')
legend('NaN removed from dataset','Location','northwest')
which produces:
untitled.jpg
So, "how" depends upon what effect one is after -- the straightforward way plot() just leaves holes where NaN elements reside--if one doesn't include them by exclusion programmatically, then the resulting plot makes look like they don't exist at all..which may or may not be kosher in displaying the data.
"It all depends!" :)

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

回答 (2 件)

Juan Carlos Ortega
Juan Carlos Ortega 2023 年 6 月 13 日
Inthis case, not work:
y=[1 3 nan 4 7]; % data array w/ an included NaN
x=[2 3 5 7 9];
subplot(2,1,1)
plot(x,y,'*-')
legend('NaN silently ignored','Location','northwest')
subplot(2,1,2)
plot(x,y(isfinite(y)),'*-')
Error using plot
Vectors must be the same length.
legend('NaN removed from dataset','Location','northwest')
  1 件のコメント
Les Beckham
Les Beckham 2023 年 6 月 13 日
編集済み: Les Beckham 2023 年 6 月 13 日
y=[1 3 nan 4 7]; % data array w/ an included NaN
x=[2 3 5 7 9];
subplot(2,1,1)
plot(x,y,'*-')
legend('NaN silently ignored','Location','northwest')
subplot(2,1,2)
plot(x(isfinite(y)),y(isfinite(y)),'*-') % use the same indexing for both x & y
legend('NaN removed from dataset','Location','northwest')

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


Juan Carlos Ortega
Juan Carlos Ortega 2023 年 6 月 14 日
Wow.. thanks a lot!

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by