Astrid
Astrid

Reputation: 1936

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.

Let

R =

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

Then in MATLAB:

>> norm(R)

ans =

     1

But in 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?

Upvotes: 4

Views: 6172

Answers (3)

moodoki
moodoki

Reputation: 109

Matlab default for matrix norm is the 2-norm while scipy and numpy's default to the Frobenius norm for matrices. Specifying the norm explicitly should fix it for you

Upvotes: 2

Amro
Amro

Reputation: 124563

Either do this in MATLAB:

>> norm(R,'fro')

or this in Python:

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

Upvotes: 7

chappjc
chappjc

Reputation: 30589

Python is returning the Frobenius norm. You can do this in MATLAB with:

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

By default, norm gives the 2-norm (norm(R,2)).

Upvotes: 8

Related Questions