2nd order polynomial fitting with NaNs

8 visualizaciones (últimos 30 días)
Nikos Makris
Nikos Makris el 24 de En. de 2011
I want to fit a 2nd order polynomial to my data
x=(1,256) y=(1,256)
Only 40 cells from each side of the y array include values, the rest are NaN. So far i have used the polyfit() function but it does not work when the y array contains NaNs. Another function is interp1() which works properly but the fitting methods are limited (no polynomial option).
Are you aware of any other function that is suitable for this problem?

Respuesta aceptada

Egon Geerardyn
Egon Geerardyn el 24 de En. de 2011
Try indexing your data points to your non-NaN-points
Let's just work on an example:
%%just generating data
x = 1:10;
y = x.^2 + randn(size(x));
y(4) = NaN; %I introducce a NaN in y
%%getting indices where y is valid (not NaN)
idxValid = ~isnan(y);
%%fitting
poly = polyfit(x(idxValid),y(idxValid),2);
%%plot
figure;
plot(x,y,'xr','DisplayName','Data points'); hold on;
plot(x,polyval(poly,x),'DisplayName','Fitted'); hold off;
legend('show')
As you will see: this fits only the non NaN data.
  3 comentarios
Egon Geerardyn
Egon Geerardyn el 24 de En. de 2011
@Nikos: it is impossible to include NaN in fitting. NaN stands for not a number (this might mean 0/0 in some cases), it really is an unknown value, so it's impossible to perform any calculation on it. How do you see a fit working when you don't know the values?
What are you actually trying to do? Do you want your polynomial to return values even for x-values where the y-value was NaN? If so, that's exactly what the code above does. That is also the most sensible case for a fit. So I hope you misunderstood my last sentence.
If you want your model to return NaN where y was NaN, you can just put these values there:
yModel = polyval(poly,x);
yModel(~idxValid) = NaN;
Nikos Makris
Nikos Makris el 25 de En. de 2011
Ok i got it! I misunderstood your 1st answer...This is what i was looking for
Thanks Egon

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Polynomials en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by