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

时间:2018-09-01 21:29:27

标签: matlab octave

public playerList: string = 'playerList';

ngOnInit() {
  this.subscription.add(this.dragulaService.drop(this.playerList)
    .subscribe(({ el, target }) => {       
      if (target.className.indexOf('substitute') > -1) {
        this.removeClass(target, 'ex-over');
      }
    })
  );

  this.subscription.add(this.dragulaService.over(this.playerList)
    .subscribe(({ el, container }) => {
      if (container.className.indexOf('substitute') > -1) {
        this.addClass(container, 'ex-over'); 
        container.innerHTML = '';       
      }   
    })
  );

  this.subscription.add(this.dragulaService.out(this.playerList)
    .subscribe(({ el, container }) => {
      this.removeClass(container, 'ex-over');
    })
  );
}

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

Output2

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

1 个答案:

答案 0 :(得分:2)

我不确定您想要的结果是什么,但是由于您正在使用[1x1]矩阵对标量([2x2]维度)进行matrix division运算,所以会出现错误。请注意,您正在执行矩阵除法(/运算符),而不是逐个元素除法(./运算符)。

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

但是,这不是唯一的问题,因为以下行的减号运算符也存在类似的问题:

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;

或者,函数可能不正确,并且由于错误地调用了函数而出现了这些错误。