Polyfit Ignoring NaN Values

34 ビュー (過去 30 日間)
Maddie Harasyn
Maddie Harasyn 2017 年 11 月 24 日
コメント済み: Maddie Harasyn 2017 年 11 月 24 日
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