MATLAB® and NumPy/SciPy have a lot in common. But there are many differences. NumPy and SciPy were created to do numerical and scientific computing in the most natural way with Python, not to be MATLAB® clones. This page is intended to be a place to collect wisdom about the differences, mostly for the purpose of helping proficient MATLAB® users become proficient NumPy and SciPy users.
MATLAB | NumPy |
---|---|
In MATLAB®, the basic data type is a multidimensional array of double precision floating point numbers. Most expressions take such arrays and return such arrays. Operations on the 2-D instances of these arrays are designed to act more or less like matrix operations in linear algebra. | n NumPy the basic type is a multidimensional array. Operations on these arrays in all dimensionalities including 2D are elementwise operations. However, there is a special matrix type for doing linear algebra, which is just a subclass of the array class. Operations on matrix-class arrays are linear algebra operations. |
MATLAB® uses 1 (one) based indexing. The initial element of a sequence is found using a(1). See note INDEXING | Python uses 0 (zero) based indexing. The initial element of a sequence is found using a[0]. |
MATLAB®’s scripting language was created for doing linear algebra. The syntax for basic matrix operations is nice and clean, but the API for adding GUIs and making full-fledged applications is more or less an afterthought. | NumPy is based on Python, which was designed from the outset to be an excellent general-purpose programming language. While Matlab’s syntax for some array manipulations is more compact than NumPy’s, NumPy (by virtue of being an add-on to Python) can do many things that Matlab just cannot, for instance subclassing the main array type to do both array and matrix math cleanly. |
In MATLAB®, arrays have pass-by-value semantics, with a lazy copy-on-write scheme to prevent actually creating copies until they are actually needed. Slice operations copy parts of the array. | In NumPy arrays have pass-by-reference semantics. Slice operations are views into an array. |
They are the standard vector/matrix/tensor type of numpy. Many numpy functions return arrays, not matrices. There is a clear distinction between element-wise operations and linear algebra operations. You can have standard vectors or row/column vectors if you like. The only disadvantage of using the array type is that you will have to use dot instead of * to multiply (reduce) two tensors (scalar product, matrix vector multiplication etc.).
NumPy contains both an array class and a matrix class. The array class is intended to be a general-purpose n-dimensional array for many kinds of numerical computing, while matrix is intended to facilitate linear algebra computations specifically. In practice there are only a handful of key differences between the two.
*
’ means element-wise multiplication, and the dot() function is used for matrix multiplication.*
’ means matrix multiplication, and the multiply() function is used for element-wise multiplication.There are pros and cons to using both:
The array is thus much more advisable to use.
NumPy has some features that facilitate the use of the matrix type, which hopefully make things easier for Matlab converts.
The table below gives rough equivalents for some common MATLAB® expressions. These are not exact equivalents, but rather should be taken as hints to get you going in the right direction. For more detail read the built-in documentation on the NumPy functions.
Some care is necessary when writing functions that take arrays or matrices as arguments — if you are expecting an array and are given a matrix, or vice versa, then ‘*’ (multiplication) will give you unexpected results. You can convert back and forth between arrays and matrices using
These functions all accept both arrays and matrices (among other things like Python lists), and thus are useful when writing functions that should accept any array-like object.
In [1]:
import numpy as np
import scipy.linalg as la
In [2]:
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
m = np.matrix([[1,2,3], [4,5,6]])
print('a=', a)
print(19*'-')
print('m=', m)
print(19*'-')
print('ndim(a) = ', np.ndim(a))
print('a.ndim = ', a.ndim)
print(19*'-')
print('np.ndim(m) = ', np.ndim(m))
print('m.ndim = ', m.ndim)
In [3]:
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
m = np.matrix([[1,2,3], [4,5,6]])
print('a=', a)
print(19*'-')
print('m=', m)
print(19*'-')
print('size(a) = ', np.size(a))
print('a.size = ', a.size)
print(19*'-')
print('np.size(m) = ', np.size(m))
print('m.size = ', m.size)
In [4]:
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
m = np.matrix([[1,2,3], [4,5,6]])
print('a=', a)
print(19*'-')
print('m=', m)
print(19*'-')
print('shape(a) = ', np.shape(a))
print('a.shape = ', a.shape)
print(19*'-')
print('np.shape(m) = ', np.shape(m))
print('m.shape = ', m.shape)
In [5]:
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
m = np.matrix([[1,2,3], [4,5,6]])
print('a=', a)
print(19*'-')
print('m=', m)
print(19*'-')
print('a.shape[2-1] = ', a.shape[2-1])
print(19*'-')
print('m.shape[2-1] = ', m.shape[2-1])
In [6]:
a = np.array([[1.,2.,3.], [4.,5.,6.]])
m = np.matrix([[1.,2.,3.], [4.,5.,6.]])
print('a=', a)
print(19*'-')
print('m=', m)
In [7]:
a = np.array([[1.,2.,3.], [4.,5.,6.]])
b = np.array([[10.,20.,30.], [40.,50.,60.]])
c = np.array([[11.,12.,13.], [14.,15.,16.]])
d = np.array([[19.,29.,39.], [49.,59.,69.]])
m = np.matrix([[1.,2.,3.], [4.,5.,6.]])
n = np.matrix([[10.,20.,30.], [40.,50.,60.]])
r = np.matrix([[11.,12.,13.], [14.,15.,16.]])
p = np.matrix([[19.,29.,39.], [49.,59.,69.]])
In [8]:
e = np.vstack([np.hstack([a,b]), np.hstack([c,d])])
q = np.vstack([np.hstack([m,n]), np.hstack([r,p])])
print ('e = ', e)
print ('q = ', q)
In [9]:
f = np.bmat('a b; c d').A
s = np.bmat('m n; r p').A
print ('f = ', f)
print ('s = ', s)
In [10]:
a = np.array([1,2,3,4])
print(a[-1])
In [11]:
a = np.array([[1.,2.,3.,4.,5.], [6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]])
m = np.matrix([[1.,2.,3.,4.,5.], [6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]])
print('a=', a)
print(39*'-')
print('m=', m)
print(39*'-')
print(a[1,4])
print(39*'-')
print(m[1,4])
In [12]:
a = np.array([[1.,2.,3.,4.,5.], [6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]])
m = np.matrix([[1.,2.,3.,4.,5.], [6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]])
print('a=', a)
print(39*'-')
print('m=', m)
print(39*'-')
print('2nd row of a: a[1] = ', a[1])
print('2nd row of a: a[1:] = ', a[1,:])
print(39*'-')
print('2nd row of m: m[1] = ', m[1])
print('2nd row of m: m[1:]', m[1,:])
In [29]:
a=np.random.rand(8,5)
print(a)
In [14]:
a[0:5]
Out[14]:
In [15]:
a[:5]
Out[15]:
In [16]:
a[0:5,:]
Out[16]:
In [17]:
m = np.mat(a) #or np.asmatrix(a)
In [18]:
m
Out[18]:
In [19]:
m[0:5]
Out[19]:
In [20]:
m[:5]
Out[20]:
In [21]:
m[0:5,:]
Out[21]:
In [23]:
a[-5:]
Out[23]:
In [24]:
m=np.mat(a)
m
Out[24]:
In [25]:
m[-5:]
Out[25]:
In [30]:
a=np.random.rand(5,10)
b= a.copy()
print(a)
In [6]:
a[0:3][:,4:9]
Out[6]:
In [7]:
a[np.ix_([1,3,4],[0,2])]
Out[7]:
In [8]:
a[2:21:2,:]
Out[8]:
In [ ]:
In [9]:
a[::2,:]
Out[9]:
In [10]:
a[ ::-1,:]
Out[10]:
In [11]:
a[np.r_[:len(a),0]]
Out[11]:
In [12]:
a.transpose()
Out[12]:
In [13]:
a.T
Out[13]:
In [14]:
a.conj().T
Out[14]:
In [15]:
a.conj().transpose()
Out[15]:
In [16]:
a.dot(np.asarray(m))
In [17]:
a*a
Out[17]:
In [18]:
a/a
Out[18]:
In [19]:
a**3
Out[19]:
In [20]:
a>0.5
Out[20]:
In [21]:
np.nonzero(a>0.5)
Out[21]:
In [22]:
v = a[:,4].T
a[:,np.nonzero(v>0.5)[0]]
Out[22]:
In [23]:
a[:,v.T>0.5]
Out[23]:
In [41]:
a = b.copy()
a[a<0.5]=0
print(a)
In [48]:
a = b.copy()
a * (a>0.5)
Out[48]:
In [52]:
a[:] = 3
a
Out[52]:
In [4]:
a=np.random.rand(2,2)
c = a.copy()
b = a
print(a), print(b)
# changing b now changes a (anf vise veras)
b[1,1] = 0
print(a)
print(b)
a[0,0] = -1
print(a)
print(b)
# changing c does not change a (and vise versa)
a[0,1] = -10
print(a)
print(c)
c[1,0] = 20
print(a)
print(c)
In [8]:
y=a[1,:]
print(a)
print(y)
a[1,1]=1
print(y)
In [9]:
a.flatten()
Out[9]:
In [10]:
a = np.arange(1,11)
In [11]:
a = np.arange(10)
In [ ]:
In [20]:
a = np.zeros((3,5))
print(a)
In [19]:
a = np.zeros((2,3,5))
print(a)
In [17]:
a = np.ones((3,4))
print(a)
In [15]:
a = np.eye(3)
print(a)
In [13]:
a=np.random.rand(4,4)
b = np.diag(a)
print(a)
print(b)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
MATLAB | Numpy |
---|---|
max(max(a)) | a.max() |
Submatrix: Assignment to a submatrix can be done with lists of indexes using the ix_ command. E.g., for 2d array a, one might do: ind=[1,3]; a[np.ix_(ind,ind)]+=100.
HELP: There is no direct equivalent of MATLAB’s which command, but the commands help and source will usually list the filename where the function is located. Python also has an inspect module (do import inspect) which provides a getfile that often works.
INDEXING: MATLAB® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the “first” element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
RANGES: In MATLAB®, 0:5 can be used as both a range literal and a ‘slice’ index (inside parentheses); however, in Python, constructs like 0:5 can only be used as a slice index (inside square brackets). Thus the somewhat quirky r object was created to allow numpy to have a similarly terse range construction mechanism. Note that r is not called like a function or a constructor, but rather indexed using square brackets, which allows the use of Python’s slice syntax in the arguments.
LOGICOPS: & or | in NumPy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab’s & or | operators, you should use the NumPy ufuncs logical_and/logical_or. The notable differences between Matlab’s and NumPy’s & and | operators are:
Non-logical {0,1} inputs: NumPy’s output is the bitwise AND of the inputs. Matlab treats any non-zero value as 1 and returns the logical AND. For example (3 & 4) in NumPy is 0, while in Matlab both 3 and 4 are considered logical true and (3 & 4) returns 1. Precedence: NumPy’s & operator is higher precedence than logical operators like < and >; Matlab’s is the reverse.
If you know you have boolean arguments, you can get away with using NumPy’s bitwise operators, but be careful with parentheses, like this: z = (x > 1) & (x < 2). The absence of NumPy operator forms of logical_and and logical_or is an unfortunate consequence of Python’s design.
RESHAPE and LINEAR INDEXING: Matlab always allows multi-dimensional arrays to be accessed using scalar or linear indices, NumPy does not. Linear indices are common in Matlab programs, e.g. find() on a matrix returns them, whereas NumPy’s find behaves differently. When converting Matlab code it might be necessary to first reshape a matrix to a linear sequence, perform some indexing operations and then reshape back. As reshape (usually) produces views onto the same storage, it should be possible to do this fairly efficiently. Note that the scan order used by reshape in NumPy defaults to the ‘C’ order, whereas Matlab uses the Fortran order. If you are simply converting to a linear sequence and back this doesn’t matter. But if you are converting reshapes from Matlab code which relies on the scan order, then this Matlab code: z = reshape(x,3,4); should become z = x.reshape(3,4,order=’F’).copy() in NumPy.
In MATLAB® the main tool available to you for customizing the environment is to modify the search path with the locations of your favorite functions. You can put such customizations into a startup script that MATLAB will run on startup.
NumPy, or rather Python, has similar facilities.
To modify your Python search path to include the locations of your own modules, define the PYTHONPATH environment variable. To have a particular script file executed when the interactive Python interpreter is started, define the PYTHONSTARTUP environment variable to contain the name of your startup script.
Unlike MATLAB®, where anything on your path can be called immediately, with Python you need to first do an ‘import’ statement to make functions in a particular file accessible.
For example you might make a startup script that looks like this (Note: this is just an example, not a statement of “best practices”):
# Make all numpy available via shorter 'num' prefix
import numpy as num
# Make all matlib functions accessible at the top level via M.func()
import numpy.matlib as M
# Make some matlib functions accessible directly at the top level via, e.g. rand(3,3)
from numpy.matlib import rand,zeros,ones,empty,eye
# Define a Hermitian function
def hermitian(A, **kwargs):
return num.transpose(A,**kwargs).conj()
# Make some shorcuts for transpose,hermitian:
# num.transpose(A) --> T(A)
# hermitian(A) --> H(A)
T = num.transpose
H = hermitian
See http://mathesaurus.sf.net/ for another MATLAB®/NumPy cross-reference.
An extensive list of tools for scientific work with python can be found in the topical software page.
MATLAB® and SimuLink® are registered trademarks of The MathWorks. Table Of Contents
NumPy for Matlab users
Introduction
Some Key Differences
‘array’ or ‘matrix’? Which should I use?
Short answer
Long answer
Facilities for Matrix Users
Table of Rough MATLAB-NumPy Equivalents
General Purpose Equivalents
Linear Algebra Equivalents
Notes
Customizing Your Environment
Links
Previous topic
Miscellaneous Next topic
Building from source
In [ ]: