Polyfit Ignoring NaN Values

37 次查看(过去 30 天)
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!

采纳的回答

Image Analyst
Image Analyst 2017-11-24
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 个评论
Maddie Harasyn
Maddie Harasyn 2017-11-24
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!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by