GNU Octave矩阵划分如何工作?获得意外行为.

eya*_*ler 4 division octave matrix-multiplication

在GNU Octave中,矩阵划分如何工作?

而不是做

1./[1;1]
Run Code Online (Sandbox Code Playgroud)

我不小心做了

1/[1;1]
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,这会产生:

[0.5, 0.5]
Run Code Online (Sandbox Code Playgroud)

横向案例:

1/[1,1]
Run Code Online (Sandbox Code Playgroud)

给出了预期的:

error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
Run Code Online (Sandbox Code Playgroud)

有人可以解释[0.5,0.5]的结果吗?

eya*_*ler 5

这是我在课程机器学习课程讨论论坛上从Alan Boulton那里得到的答案:

这个想法的要点是x/y被非常普遍地定义,以便它可以处理矩阵.从概念上讲,/运算符正在尝试返回x*y-1(或Octave-speak中的x*inv(y)),如下例所示:

octave:1> eye(2)/[1 2;3 4]
ans =
  -2.00000   1.00000
   1.50000  -0.50000

octave:2> inv([1 2;3 4])
ans =
  -2.00000   1.00000
   1.50000  -0.50000
Run Code Online (Sandbox Code Playgroud)

当y是列向量时会发生棘手,在这种情况下,inv(y)是未定义的,因此使用pinv(y),即y的伪逆.

octave:1> pinv([1;2])
ans =
   0.20000   0.40000

octave:2> 1/[1;2]
ans =
   0.20000   0.40000
Run Code Online (Sandbox Code Playgroud)

向量y需要与x兼容,以便明确定义x*pinv(y).因此,如果y是行向量,只要x兼容即可.有关说明,请参阅以下Octave会话:

octave:18> pinv([1 2])
ans =
   0.20000
   0.40000

octave:19> 1/[1 2]
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
octave:19> eye(2)/[1 2]
ans =
   0.20000
   0.40000

octave:20> eye(2)/[1;2]
error: operator /: nonconformant arguments (op1 is 2x2, op2 is 2x1)
octave:20> 1/[1;2]
ans =
   0.20000   0.40000
Run Code Online (Sandbox Code Playgroud)


小智 5

考虑以下示例:

>> A = [5 10];

>> B = [2 2];
Run Code Online (Sandbox Code Playgroud)

如果要按元素进行除法,则在两个元素的矩阵大小相等的情况下使用A ./ B,即如果A的大小为m?n B的大小必须为m?n

>> A ./B
ans =

    2.5000   5.0000
Run Code Online (Sandbox Code Playgroud)

如果要按矩阵划分矩阵,请使用A / B,将元素A的矩阵大小设为m?n,将B设为q?n或m?n。/运算符试图返回x?y?1(即x * pinv(y)以八度为格式)。

>> A / B
ans =  3.7500
Run Code Online (Sandbox Code Playgroud)

与...相同

>> A * pinv(B)
ans =  3.7500
Run Code Online (Sandbox Code Playgroud)

OCTAVE / MATLAB中的pinv()函数返回矩阵的Moore-Penrose伪逆,而inv()函数返回矩阵的逆。如果您对使用什么感到困惑,请使用pinv()如果您想进一步澄清pinv和inv之间的区别是什么?