How to Ignore NaN values when plotting?
178 次查看(过去 30 天)
显示 更早的评论
Hi, If anyone can tell me how to ignore NaN values when you are plotting? Thanks
2 个评论
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:

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
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)),'*-')
legend('NaN removed from dataset','Location','northwest')
1 个评论
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

