Polyfit Ignoring NaN Values
34 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!