Help with error in fit re NaN and Inf values

28 views (last 30 days)
I'm hoping someone can help me determine why I'm getting the following error:
Error using fit>iFit (line 415)
Input to EIG must not contain NaN or Inf.
Error in fit (line 109)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in mainnew>oneload (line 172)
powerfit=fit(x2,y2,'power1');
The code is as follows. I start with two arrays, of the same length. Each y value has an associated x value, and some of the y values are occasionally NaN. They should never be Inf. I want to do a power fit on the valid y values, so I cut the NaN and Inf values out. Then I do a fit.
y1=y(~isnan(y));
y2=y1(~isinf(y1));
x1=x(~isnan(y));
x2=x1(~isinf(y1));
if length(y2)>1
powerfit=fit(x2,y2,'power1');
end
Given that I have just removed all the NaN and Inf values, I'm baffled as to why I'm being told I can't input NaN or Inf, since I just removed all those terms. I am also unclear as to what EIG is, because the function eig() is not called in iFit to the best of my knowledge. It appears to me that something is failing between lines 359 and 410 of iFit, I just don't know what nor why, and would appreciate any help.

Accepted Answer

Stephen23
Stephen23 on 14 Jan 2015
Edited: Stephen23 on 14 Jan 2015
It may be that your data still includes some non-finite values. When bug-hunting it does not matter that you think there are no NaN's or Inf's, but only that there are some NaN's or Inf's: print the data exactly as it is entered into the function fit , and see what it looks like.
To be most consistent you should probably remove the corresponding data points both of your x and y vectors (otherwise they could end up different sizes), so you could try something like this:
>> x = [1;2;3;NaN;5;6;Inf;8;9];
>> y = [9;8;Inf;6;5;4;NaN;2;Inf];
>> idx = isfinite(x) & isfinite(y);
>> pwf = fit(x(idx),y(idx),'poly1')
Note that isfinite(X) is equivalent to ~isinf(X)&~isnan(X).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!