MATLAB中的mrdivide函数:它在做什么,如何在Python中做到这一点? [英] mrdivide function in MATLAB: what is it doing, and how can I do it in Python?

查看:386
本文介绍了MATLAB中的mrdivide函数:它在做什么,如何在Python中做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行MATLAB代码:

I have this line of MATLAB code:

a/b

我正在使用以下输入:

a = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]   
b = ones(25, 18)

这是结果(1x25矩阵):

This is the result (a 1x25 matrix):

[5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

MATLAB在做什么?我试图在Python中复制此行为,并且MATLAB中的mrdivide文档无济于事. 5从何而来,为什么其余的值都为0?

What is MATLAB doing? I am trying to duplicate this behavior in Python, and the mrdivide documentation in MATLAB was unhelpful. Where does the 5 come from, and why are the rest of the values 0?

我已经用其他输入尝试了此操作,并且得到了相似的结果,通常只是一个不同的第一个元素和零填充了矩阵的其余部分.在Python中,当我使用linalg.lstsq(b.T,a.T)时,返回的第一个矩阵中的所有值(即非奇异值)均为0.2.我已经在Python中尝试了正确的除法,但是由于尺寸错误,它会完全消失.

I have tried this with other inputs and receive similar results, usually just a different first element and zeros filling the remainder of the matrix. In Python when I use linalg.lstsq(b.T,a.T), all of the values in the first matrix returned (i.e. not the singular one) are 0.2. I have already tried right division in Python and it gives something completely off with the wrong dimensions.

我了解最小二乘近似是什么,我只需要知道mrdivide在做什么.

I understand what a least square approximation is, I just need to know what mrdivide is doing.

推荐答案

MRDIVIDE /运算符实际上解决了xb = a线性系统,而不是

MRDIVIDE or the / operator actually solves the xb = a linear system, as opposed to MLDIVIDE or the \ operator which will solve the system bx = a.

要使用非对称,不可逆矩阵b求解系统xb = a,可以依赖mridivide()(通过对带有高斯消除的b进行因式分解)或pinv() ,这是通过奇异值分解以及将奇异值归零到(默认)公差级别以下来完成的.

To solve a system xb = a with a non-symmetric, non-invertible matrix b, you can either rely on mridivide(), which is done via factorization of b with Gauss elimination, or pinv(), which is done via Singular Value Decomposition, and zero-ing of the singular values below a (default) tolerance level.

这是区别(对于mldivide):

当系统超定时,两种算法都会提供 相同的答案.当系统不确定时,PINV将返回 具有最小范数(min NORM(x))的解x. MLDIVIDE将 选择具有最少非零元素数量的解决方案.

When the system is overdetermined, both algorithms provide the same answer. When the system is underdetermined, PINV will return the solution x, that has the minimum norm (min NORM(x)). MLDIVIDE will pick the solution with least number of non-zero elements.

在您的示例中:

% solve xb = a
a = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9];
b = ones(25, 18);

系统尚未确定,两个不同的解决方案将是:

the system is underdetermined, and the two different solutions will be:

x1 = a/b; % MRDIVIDE: sparsest solution (min L0 norm) 
x2 = a*pinv(b); % PINV: minimum norm solution (min L2)

>> x1 = a/b
Warning: Rank deficient, rank = 1,  tol = 2.3551e-014.
ans =

    5.0000 0 0 ... 0 

>> x2 = a*pinv(b)
ans =

    0.2 0.2 0.2 ... 0.2 

在两种情况下,xb-a的近似误差都是不可忽略的(非精确解)并且是相同的,即norm(x1*b-a)norm(x2*b-a)将返回相同的结果.

In both cases the approximation error of xb-a is non-negligible (non-exact solution) and the same, i.e. norm(x1*b-a) and norm(x2*b-a) will return the same result.

MATLAB在做什么?

此帖子中

A great break-down of the algorithms (and checks on properties) invoked by the '\' operator, depending upon the structure of matrix b is given in this post in scicomp.stackexchange.com. I am assuming similar options apply for the / operator.

在您的示例中,MATLAB很可能会进行高斯消除,从而在无穷大(这就是5的来源)中给出了最稀疏的解决方案.

For your example, MATLAB is most probably doing a Gaussian elimination, giving the sparsest solution amongst a infinitude (that's where the 5 comes from).

Python在做什么?

Python使用伪逆/SVD(这就是为什么得到0.2的向量)的原因.实际上,以下内容将为您提供与MATLAB pinv()相同的结果:

Python, in linalg.lstsq uses pseudo-inverse/SVD, as demonstrated above (that's why you get a vector of 0.2's). In effect, the following will both give you the same result as MATLAB's pinv():

from numpy import *

a = array([1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9])
b = ones((25, 18))

# xb = a: solve b.T x.T = a.T instead 
x2 = linalg.lstsq(b.T, a.T)[0]
x2 = dot(a, linalg.pinv(b)) 

这篇关于MATLAB中的mrdivide函数:它在做什么,如何在Python中做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆