为什么Matlab在评估数组时错误地返回0,但在评估标量时正确返回NaN?

Swy*_*dct 4 matlab nan

为什么h(theta)下面的表达式有时会返回0并且有时会返回NaNh(theta)包含零除以theta = 0,应始终返回NaN.如果我只是要求h(0),一切正常.

但是,当它评估多元素数组中包含的零时,它将在返回h = 0时返回NaN.但是如果只专门评估零元素,那么NaN它就应该返回.

>> theta = [0 1]
theta =
     0     1
Run Code Online (Sandbox Code Playgroud)

第一个元素应该是NaN:

>> h = tan(theta)/(1+(tan(theta)/tan(2.*theta)))
h =
     0    5.4220 
Run Code Online (Sandbox Code Playgroud)

在具体评估零元素时,它可以正常工作:

>> h = tan(theta(1))/(1+(tan(theta(1))/tan(2.*theta(1))))
h =
     NaN 

>> h = tan(theta(2))/(1+(tan(theta(2))/tan(2.*theta(2))))
h =
     5.4220
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 5

您需要进行元素划分./而不是/获得所需的结果.这称为正确的阵列划分.

>> h = tan(theta)./(1+(tan(theta)./tan(2.*theta)))
h =
       NaN    5.4220
Run Code Online (Sandbox Code Playgroud)