简体   繁体   English

使用MATLAB和Python规范函数获得不同的答案

[英]Getting different answers with MATLAB and Python norm functions

I am getting two vastly different answers with regards to simple matrix norms when comparing the MATLAB and Python functions. 在比较MATLAB和Python函数时,我得到了关于简单矩阵规范的两个截然不同的答案。

Let

R =

    0.9940    0.0773   -0.0773
   -0.0713    0.9945    0.0769
    0.0828   -0.0709    0.9940

Then in MATLAB: 然后在MATLAB中:

>> norm(R)

ans =

     1

But in Python 但在Python中

from scipy.linalg import norm
import numpy as np

print norm(R),np.linalg.norm(R)

1.73205080757 1.73205080757

where 哪里

print scipy.__version__,np.__version__
0.14.0 1.9.0

How did I manage to so comprehensively screw that up? 我是如何全面搞砸的呢?

Python is returning the Frobenius norm. Python正在回归Frobenius规范。 You can do this in MATLAB with: 您可以在MATLAB中执行以下操作:

>> norm(R,'fro')
ans =
          1.73203140271763

By default, norm gives the 2-norm ( norm(R,2) ). 默认情况下, norm给出2范数( norm(R,2) )。

Either do this in MATLAB: 要么在MATLAB中这样做:

>> norm(R,'fro')

or this in Python: 或者在Python中:

>>> np.linalg.norm(R,2)

Matlab default for matrix norm is the 2-norm while scipy and numpy's default to the Frobenius norm for matrices. 矩阵范数的Matlab默认值是2范数,而scipy和numpy默认为矩阵的Frobenius范数。 Specifying the norm explicitly should fix it for you 明确指定规范应该为您解决

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

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