本文介绍了interp1返回x范围内的最后一个值的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MATLAB中的 interp1 函数有疑问.设x在[1,1024]范围内.但是,以下内容:

I have a question about the interp1 function in MATLAB. Let x be in the range [1, 1024]. However, the following:

yi = interp1(x, y, 1024);

返回NaN.

我已经检查了我的程序几次,似乎没有什么错,但是结果对我来说仍然很奇怪.如果有人能帮助我,我会很感激.

I've checked my program several times and nothing seems wrong but the result is still weird to me. I'd be thankful if anyone could help.

推荐答案

我很确定您不会达到1024,而是有些值不如1023.999999999945435

I'm pretty sure that you're not getting to 1024 but rather some value just less like 1023.999999999945435

尝试这个非常人为的示例:

Try out this very contrived example:

x = 1:10;
y = 1:10;
interp1(x, y, 10) %This works, returns 10

现在尝试

x = 1:0.99999999768768765:10 %note x(end) will give you 10.0000, very deceptive because sum(x == 10 ) gives 0
interp(x, y, 10) %Returns NaN

因此,尽管我的示例非常人为,但有时可能会由于精确度错误而发生,即使您指定的序列应该精确到1024,也不是因为用二进制表示漂亮的十进制数而被截断了,而没有看起来更好.

So although my example is very contrived, sometimes it could happen that due to precision errors even though the series you specified should get exactly to 1024 it doesn't because of the truncation of representing nice looking decimal number in binary where they no longer look nice.

如果这是您的问题,则可以将x舍入为适当的精度,或者可以在interp1选项中指定外推法,但是我认为舍入会更好,因为它将停止其他意外的精度问题.

If this is your issue then either round off x to the appropriate precision or else you could specify extrapolation in the interp1 options but I would think rounding is better because it will stop other unexpected precision issue down the line.

这篇关于interp1返回x范围内的最后一个值的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:31