Polyfit Ignoring NaN Values

34 views (last 30 days)
Maddie Harasyn
Maddie Harasyn on 24 Nov 2017
Commented: Maddie Harasyn on 24 Nov 2017
Hello!
I am trying to iterate over rows of a matrix (Z) and fit a curve to each row and then subtract the fitted curve from the original data, however all of my rows contain NaN values. I would like to somehow fit the curve ignoring the NaN values, to subtract this curve from my original matrix row of values. The result would be a matrix with the same dimensions as my original, with the curve subtracted from real values and NaN values in their original place holders. I have thought about indexing NaN values using isnan() and then applying the polyfit to values when isnan() == 0 but everything I've tried so far hasn't quite worked out.
This is what it looks like so far:
for j = 1:m;
nx = size(Z,2);
x = 1:nx;
p = polyfit(x,Z(j,:),2);
y = polyval(p,x)
Z(j,:) = Z(j,:) - y;
end
Thanks for your help!

Accepted Answer

Image Analyst
Image Analyst on 24 Nov 2017
Can you attach Z in a .mat file so we can try things? Like
for j = 1:m;
thisRow = Z(j, :);
nx = size(Z,2);
x = 1:nx;
nanIndexes = isnan(thisRow);
thisRow(nanIndexes) = [];
x2 = x(~nanIndexes)
p = polyfit(x2,thisRow,2);
y = polyval(p,x)
Z(j,:) = Z(j,:) - y;
end
but that's just off the top of my head. would be easier if we had your Z.
  1 Comment
Maddie Harasyn
Maddie Harasyn on 24 Nov 2017
This works perfectly! I tried to attach my matrix but it was deemed too large, as it is a DEM ASCII file of a large drone survey I ran. I think I was having trouble, as I am used to Python and was trying to complete this with embedded for and if loops.
Thanks so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!