简体   繁体   English

为什么此高斯函数会给出不一致的参数错误?

[英]Why does this Gaussian function give nonconformant arguments error?

function m=gaussian(med, var, n)
  if ( mod(n, 2)==0 )
      n=n+1;
  end;

  med=double(med);
  var=double(var);

  med = min (max(-(n+1)/2, med),  (n+1)/2);

  m=zeros(1,n);

  k1=(1/(2*pi*var)^0.5);
  k2=-0.5.*((med-(1:n)).^2)./var;

  m(1,1:n)=k1.*exp(k2);

Output1 输出1

>> gaussian([101 2 ; 3 4], [4 301 ; 2 1], [2 2])
error: gaussian: operator /: nonconformant arguments (op1 is 1x1, op2 is 2x2)
error: called from
    gaussian at line 13 column 5
>>

Output2 输出2

>> gaussian([101 2 ; 3 4], [4 301 ; 2 1], 2)
error: gaussian: operator /: nonconformant arguments (op1 is 1x1, op2 is 2x2)
error: called from
    gaussian at line 13 column 5

I'm unsure what you wanted the result to be but you get that error because you're doing matrix division of a scalar ( [1x1] dimensions) by a [2x2] matrix. 我不确定您想要的结果是什么,但是会收到该错误,因为您正在用[2x2]矩阵对标量( [1x1]尺寸)进行矩阵除法 Note, you're doing matrix division ( / operator) and not a element by element division ( ./ operator). 请注意,您正在执行矩阵除法( /运算符),而不是逐个元素除法( ./运算符)。

octave:1> function m=gaussian(med, var, n)
>   if ( mod(n, 2)==0 )
>       n=n+1;
>   end;
> 
>   med=double(med);
>   var=double(var);
> 
>   med = min (max(-(n+1)/2, med),  (n+1)/2);
> 
>   m=zeros(1,n);
> 
>   k1=(1/(2*pi*var)^0.5);
>   k2=-0.5.*((med-(1:n)).^2)./var;
> 
>   m(1,1:n)=k1.*exp(k2);
> endfunction
octave:2> debug_on_error (1)
octave:3> gaussian ([101 2 ; 3 4], [4 301 ; 2 1], 2)
error: gaussian: operator /: nonconformant arguments (op1 is 1x1, op2 is 2x2)
error: called from
    gaussian at line 13 column 5
stopped in gaussian at line 13
13:   k1=(1/(2*pi*var)^0.5);
debug> (2*pi*var)
ans =

     25.1327   1891.2388
     12.5664      6.2832

debug> 1/(2*pi*var) # matrix division
error: gaussian: operator /: nonconformant arguments (op1 is 1x1, op2 is 2x2)
error: called from
    gaussian at line 13 column 5
debug> 1./(2*pi*var) # your element by element division works
ans =

   0.03978874   0.00052875
   0.07957747   0.15915494

However, that's not the only issue since the following line has a similar issue for the minus operator: 但是,这不是唯一的问题,因为以下行的减号运算符也有类似的问题:

error: gaussian: operator -: nonconformant arguments (op1 is 2x2, op2 is 1x3)
error: called from
    gaussian at line 14 column 5
stopped in gaussian at line 14
14:   k2=-0.5.*((med-(1:n)).^2)./var;

Alternatively, maybe the functions is not incorrect, and you're getting those errors because you're calling the function incorrectly. 或者,函数可能不正确,并且由于错误地调用了函数而出现了这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM