Normpdf is behaving strangely

Thus,

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

      

The following error message appears:

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 order:

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

      

What's going on here?

+3


source to share


1 answer


I'm willing to bet significant amounts of money if the problem is with the type of your input pIx5

. Note:

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

      

Why is it giving a size error for what should be type related? Taking a look at the code normpdf

will answer this. 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

      



Basically, any error in evaluating this equation is reported as a size mismatch error. In this case, the real problem with exp

not working for integer data types (it only supports 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'.

      

Solution then? Just leave your offending inputs first, single

or double

:

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

      

+8


source







All Articles