How are NaN values dealt with smooth function from Curve Fitting Toolbox

33 views (last 30 days)
With the following 1D vector "data" (truncated to their first 10 elements):
NaN NaN NaN NaN 2.8154 2.8476 2.8799 2.9121 2.9443 2.9491
I get smooth(data, 4)=
2.6866 2.7188 2.7510 2.7832 2.8154 2.8476 2.8799 2.9121 2.9364 2.9491
The documentation does not say anything about the default behaviour with NaN values. Here, the NaN are at the edges, and the values are not what the documentation says about happens at the edges.
_The first few elements of yy are given by
yy(1) = y(1) yy(2) = (y(1) + y(2) + y(3))/3 yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5 yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5_
How come I do end up with values? Are there some extrapolation for NaN at the edges? If yes, which kind?
Thank you
  2 Comments
Reko Hynönen
Reko Hynönen on 29 Nov 2017
Sorry for posting on an old topic, but it's totally ignored and I find it silly to post another question of the same essence.
So, you're correct that something is up with smooth. I can't help you with what it is, but I can showcase the problem even more. Consider this code:
x=rand(128,1); % 128 random numbers
y=movmean(x,5); % simple moving mean of 5 points
z=smooth(x); % default span is 5 points, equal to y
y(45)==z(45) % will display true
x(90)=NaN; % change a number far away from the point in question to NaN
y=movmean(x,5); % it's the same as before except for NaNs around position 90
z=smooth(x); % slightly different than original z everywhere
y(45)==z(45) % will display false
What gives? What does smooth function do with NaNs, if it's supposed to be (in its default form) a simple moving average?
Lima Kilo
Lima Kilo on 15 Jul 2021
The problem still exists as of R2020b (according to the support, they are "working on it"). Looking into the code of the smooth function, the reason for this behavior is a subfunction that is supposed to check if the input data is uniformly spaced. It changes the smoothing method from moving average to Lowess when it is nonuniform, but as a side effect also if it contains NaNs.
If you explicitely tell the function to use the "moving" method, it works as expected.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!