Interp1 returns NaN for a value

307 ビュー (過去 30 日間)
Matthew Hunt
Matthew Hunt 2019 年 10 月 8 日
編集済み: Stephen23 2023 年 11 月 21 日
I have some data (x,f(x)) where 0<=x<=1 and I have N points. I wish to obtain f for an inteverval [a,b] which is well within the limits of 0 and 1, and on my end point, I get a NaN so some reason. I think my data set goes up from 0.001 to 0.999, and the set I want values for goes between 0.05 and 0.96. There is no extrapolation required.
What's going on exactly?
  9 件のコメント
Michael Demkowicz
Michael Demkowicz 2023 年 11 月 21 日
I've run into the same problem: interp1 returns a NaN at the endpoint of the interpolation range. I'm attaching the data to demonstrate the problem. I see NaN when I load the data and run the following commands:
nirrhor=interp1(irr,irrhor,r);
nirrhor(length(r))
Stephen23
Stephen23 2023 年 11 月 21 日
編集済み: Stephen23 2023 年 11 月 21 日
"I've run into the same problem: interp1 returns a NaN at the endpoint of the interpolation range. I'm attaching the data to demonstrate the problem. I see NaN when I load the data and run the following commands"
Lets take a look:
S = load('interpdata.mat')
S = struct with fields:
irr: [10001×1 double] irrhor: [10001×1 double] r: [0 5.1557e-04 0.0010 0.0015 0.0021 0.0026 0.0031 0.0036 0.0041 0.0046 0.0052 0.0057 0.0062 0.0067 0.0072 0.0077 0.0082 0.0088 0.0093 0.0098 0.0103 0.0108 0.0113 … ] (1×11923 double)
V = interp1(S.irr,S.irrhor,S.r)
V = 1×11923
-0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058
X = find(isnan(V))
X = 11923
fprintf('%.42f %.42f\n', S.r([1,X]), S.irr([1,end])) % requested; supplied
0.000000000000000000000000000000000000000000 6.146600000000000285638179775560274720191956 0.000000000000000000000000000000000000000000 6.146599999999999397459760075435042381286621
The last value you requested clearly lies outside the domain of your data. It is unclear what the problem is, everything seems to be working exactly as documented and expected.

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2019 年 10 月 8 日
You could have NaN values in your data. If so, first use the fillmissing function (R2016b and later reseases), then interp1.
  2 件のコメント
Matthew Hunt
Matthew Hunt 2019 年 10 月 8 日
I checked and there are no problems with the data. There are some non-unique points which I removed but that shouldn't matter.
Star Strider
Star Strider 2019 年 10 月 8 日
If you are not extrapolating (that should only produce NaN values for the extrapolated values if you do not include a method designation and the 'extrap' argument), and there are no NaN values anywhere in your data, and we can’t see your data to test it, this will likely remain unsolved.

サインインしてコメントする。


Chris B
Chris B 2022 年 8 月 27 日
移動済み: Image Analyst 2022 年 8 月 27 日
I ran into an issue where my start and endpoints were NaN. Turns out I was defining my x and xquery values differently. For example, try this code:
x=[500:2:1100]*10^-1;
xq=[500*10^-1 :2*10^-1: 1100*10^-1];
x==xq
x ~= xq because you are comparing floats in this case. Change 10^-1 to 10^1 and you'll get x==xq. So if you're getting NaNs, try making your x and xquery both integers and then dividing to get floats afterward.

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by