Skipping empty cells when fitting data
2 views (last 30 days)
Show older comments
I'm trying to fit a set of data. As an example, the data is imported from a text file and would look something like this:
A = [1 2 3 4 5; 1 NaN 9 16 NaN; 2 3 4 5 6]
The "NaN" represent an empty data point or cell. After I import the data, the empty cells are replaced by NaN.
Then when I try to fit the 2nd column against the 1st column using lsqcurvefit:
lsqcurvefit(function, initial, A(:,1),A(:,2),lower,upper)
I have problems. It fits fine when I fit the 3rd column with the 1st column, so I do not think my error is in the syntax. It must be because of the "NaN." The error message says
??? Error using ==> snls at 46 Objective function is returning undefined values at initial point.lsqcurvefit cannot continue.
When I try to calculate the average value:
mean(A(:,2))
I get
ans = NaN.
How do I make Matlab skip over these empty cells NaN when fitting and performing other calculations? Or how do I import the data to prevent the NaN from appearing?
0 Comments
Answers (1)
the cyclist
on 14 Feb 2012
How individual MATLAB function handle NaNs varies on a case-by-case basis (although usually with a good rationale behind it). For example, the help file for corrcoef() describes an option for how to deal with NaNs. In the case the of mean() function, there is simply a nanmean() function that ignores NaNs.
I do not see any mention of NaN in the documentation for lsqcurvefit(), so I'm guessing you'll have to deal with it on the input side. One option is to simply exclude the columns with NaNs:
A(:,any(isnan(A),1)) = [];
I don't know if that makes sense for your data.
0 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!