normpdf表现异常 [英] normpdf behaves strangely
问题描述
通过以下方式,
function ret = f(pIx5, dS)
sigma = 1;
rho = dS(1);
theta = dS(2);
mu_x = rho*cos(theta);
display(pIx5);
display(mu_x);
pdf = normpdf(pIx5, mu_x, sigma);
ret = max(pdf);
end
我收到以下错误消息,
pIx5 =
54 65 11 0 0
mu_x =
11.9218
Error using normpdf (line 36) Non-scalar arguments must match in size.
Error in f (line 11)
pdf = normpdf(pIx5, mu_x, sigma);
但是,它可以通过以下方式正常工作,
But, it works fine in the following manner,
function ret = f(pIx5, dS)
sigma = 1;
rho = dS(1);
theta = dS(2);
pIx5 = [54,65, 11, 0, 0];
mu_x = 11.9218;
display(pIx5);
display(mu_x);
pdf = normpdf(pIx5, mu_x, sigma);
ret = max(pdf);
end
这是怎么回事?
推荐答案
I'm willing to bet significant amounts of money that the problem is with the type of your input pIx5
. Note this:
>> pdf = normpdf([54 65 11 0 0], 11.9218, 1); % Works fine
>> pdf = normpdf(uint8([54 65 11 0 0]), 11.9218, 1);
Error using normpdf (line 36)
Non-scalar arguments must match in size.
为什么与类型有关的东西会出现尺寸错误?看一下 normpdf
的代码,答案就是如此.从R2016b的第33-37行开始:
Why does it give a size error for what should be something related to the type? Taking a look at the code for normpdf
answers that. From lines 33-37, R2016b:
...
try
y = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
catch
error(message('stats:normpdf:InputSizeMismatch'));
end
基本上,将评估该方程式时的任何错误报告为大小不匹配错误.在这种情况下,实际上是 exp
无效的问题对于整数数据类型(仅支持single
和double
类型):
Basically, any error in evaluating that equation is reported as a size mismatch error. In this case, it is actually an issue with exp
not working for integer data types (it only support single
and double
types):
>> x = uint8([54 65 11 0 0]);
>> mu = 11.9218;
>> sigma = 1;
>> y = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
Undefined function 'exp' for input arguments of type 'uint8'.
那解决办法呢?只需将有问题的输入首先投射到single
或double
:
The solution then? Just cast your offending inputs to single
or double
first:
pdf = normpdf(double(pIx5), mu_x, sigma);
这篇关于normpdf表现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!