在if和interp1命令中使用NaN

时间:2017-03-25 11:33:06

标签: matlab if-statement interpolation

我在下面的代码中使用'isnan'命令时遇到问题。

我有6个值,其中一些是NaN。如果'interp1'命令发现最终值为NaN以给出'c',MATLAB必须给我错误信息。

a=[1 2 3 4 5 6];
b=[10 15 20 NaN NaN NaN];
c=[1.5 4 3.5 4.5 5.1 5.9];
for g=1:1:numel(a)
if interp1(a,b,c(g))==NaN;
        disp('There is a mistake here')
   end
end
但是,MATLAB不会使用NaN运行此命令。

*In interp1 at 178
Warning: NaN found in Y, interpolation at undefined values
     will result in undefined values.* 

我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:0)

你的matlab版本是什么?

此代码适用于我(matlab R2013b):

a=[1 2 3 4 5 6];
b=[10 15 20 NaN NaN NaN];
c=[1.5 4 3.5 4.5 5.1 5.9];
for g=1:1:numel(a)
if isnan(interp1(a,b,c(g)))
        disp('There is a mistake here')
   end
end
相关问题