normpdf表现得很奇怪

时间:2017-06-26 15:31:51

标签: matlab probability-density

以下列方式,

require 'pg'

conn = PG.connect(...)
res  = conn.exec('SELECT bytea_column FROM some_table')  
res.each do |r|
    raw = r['bytea_column']
    binary_data = PG::Connection.unescape_bytea(raw)
    puts "#{binary_data}"
end

我收到以下错误消息,

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);

这里发生了什么?

1 个答案:

答案 0 :(得分:8)

我愿意投入大量资金来解决问题与您输入的type pIx5相关的问题。请注意:

>> 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答案的代码。从第33-37行,R2016b:

...
try
    y = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
catch
   error(message('stats:normpdf:InputSizeMismatch'));
end

基本上,任何错误在评估该等式时都会报告为大小不匹配错误。在这种情况下,exp不适用于整数数据类型(它仅支持singledouble类型)实际上是一个问题:

>> 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'.

解决方案呢?只需先将您的违规输入转换为singledouble

pdf = normpdf(double(pIx5), mu_x, sigma);