General Purpose Equivalents
MATLAB numpy Notes
help func get help on the
info(func) or help(func) or func? (in Ipython)
function func
find out
which func see note HELP where func is
defined
print source
type func source(func) or func?? (in Ipython) for func (if not a
native function)
short-circuiting
logical AND operator
a && b a and b (Python native
operator); scalar
arguments only
short-circuiting
logical OR operator
a || b a or b (Python native
operator); scalar
arguments only
1*i, 1*j, 1i, 1j 1j complex numbers
Distance between 1
eps np.spacing(1) and the nearest
floating point
number.
integrate an ODE
ode45 scipy.integrate.solve_ivp(f) with Runge-Kutta
4,5
ode15s scipy.integrate.solve_ivp(f, method='BDF') integrate an ODE
with BDF method
Linear Algebra Equivalents
MATLAB NumPy Notes
get the number of
ndims(a) ndim(a) or a.ndim dimensions of an
array
get the number of
numel(a) size(a) or a.size elements of an
array
size(a) get the “size” of the
shape(a) or a.shape
matrix
get the number of
elements of the n-th
dimension of
array a. (Note that
size(a,n) a.shape[n-1] MATLAB® uses 1
based indexing
while Python uses 0
based indexing,
See
note INDEXING)
[ 1 2 3; 4 5 6 ] array([[1.,2.,3.], [4.,5.,6.]]) 2x3 matrix literal
MATLAB NumPy Notes
construct a matrix
[ a b; c d ] block([[a,b], [c,d]]) from blocks a, b, c,
and d
access last element
a(end) a[-1]
in the 1xn matrix a
access element in
a(2,5) a[1,4] second row, fifth
column
entire second row
a(2,:) a[1] or a[1,:]
of a
the first five rows
a(1:5,:) a[0:5] or a[:5] or a[0:5,:]
of a
the last five rows
a(end-4:end,:) a[-5:]
of a
rows one to three
and columns five to
a(1:3,5:9) a[0:3][:,4:9]
nine of a. This gives
read-only access.
rows 2,4 and 5 and
columns 1 and 3.
This allows the
a([2,4,5],[1,3]) a[ix_([1,3,4],[0,2])] matrix to be
modified, and
doesn’t require a
regular slice.
every other row
of a, starting with
a(3:2:21,:) a[ 2:21:2,:]
the third and going
to the twenty-first
every other row
a(1:2:end,:) a[ ::2,:] of a, starting with
the first
a(end:- a with rows in
a[ ::-1,:]
1:1,:) or flipud(a) reverse order
a with copy of the
a([1:end 1],:) a[r_[:len(a),0]] first row appended
to the end
a.' a.transpose() or a.T transpose of a
conjugate transpose
a' a.conj().transpose() or a.conj().T
of a
a * b a @ b matrix multiply
a .* b a * b element-wise
multiply
a./b a/b element-wise divide
a.^3 a**3 element-wise
exponentiation
matrix whose i,jth
element is (a_ij >
0.5). The Matlab
(a>0.5) (a>0.5) result is an array of
0s and 1s. The
NumPy result is an
array of the boolean
MATLAB NumPy Notes
values False and T
rue.
find the indices
find(a>0.5) nonzero(a>0.5)
where (a > 0.5)
extract the columms
a(:,find(v>0.5)) a[:,nonzero(v>0.5)[0]] of a where vector v
> 0.5
extract the columms
a(:,find(v>0.5)) a[:,v.T>0.5] of a where column
vector v > 0.5
a with elements less
a(a<0.5)=0 a[a<0.5]=0
than 0.5 zeroed out
a with elements less
a .* (a>0.5) a * (a>0.5)
than 0.5 zeroed out
a(:) = 3 a[:] = 3 set all values to the
same scalar value
y=x y = x.copy() numpy assigns by
reference
y=x(2,:) y = x[1,:].copy() numpy slices are by
reference
turn array into
y=x(:) y = x.flatten() vector (note that
this forces a copy)
create an increasing
arange(1.,11.) or r_[1.:11.] orr_[1:10:1
1:10 vector (see
0j]
note RANGES)
create an increasing
0:9 arange(10.) or r_[:10.] orr_[:9:10j] vector (see
note RANGES)
[1:10]' arange(1.,11.)[:, newaxis] create a column
vector
3x4 two-
zeros(3,4) zeros((3,4)) dimensional array
full of 64-bit floating
point zeros
3x4x5 three-
zeros(3,4,5) zeros((3,4,5)) dimensional array
full of 64-bit floating
point zeros
3x4 two-
ones(3,4) ones((3,4)) dimensional array
full of 64-bit floating
point ones
eye(3) eye(3) 3x3 identity matrix
vector of diagonal
diag(a) diag(a)
elements of a
square diagonal
matrix whose
diag(a,0) diag(a,0)
nonzero values are
the elements of a
rand(3,4) random.rand(3,4) random 3x4 matrix
MATLAB NumPy Notes
4 equally spaced
linspace(1,3,4) linspace(1,3,4) samples between 1
and 3, inclusive
[x,y]=meshgrid(0: two 2D arrays: one
mgrid[0:9.,0:6.] or meshgrid(r_[0:9.],r
8,0:5) of x values, the
_[0:6.]
other of y values
ogrid[0:9.,0:6.] or ix_(r_[0:9.],r_[0:6 the best way to eval
.] functions on a grid
[x,y]=meshgrid([1
meshgrid([1,2,4],[2,4,5])
,2,4],[2,4,5])
ix_([1,2,4],[2,4,5]) the best way to eval
functions on a grid
create m by n
repmat(a, m, n) tile(a, (m, n))
copies of a
concatenate((a,b),1) or hstack((a,b)) or concatenate
[a b]
column_stack((a,b)) or c_[a,b] columns of a and b
concatenate((a,b)) or vstack((a,b)) or r concatenate rows
[a; b]
_[a,b] of a and b
maximum element
of a (with
max(max(a)) a.max()
ndims(a)<=2 for
matlab)
maximum element
max(a) a.max(0) of each column of
matrix a
maximum element
max(a,[],2) a.max(1) of each row of
matrix a
compares a and b e
lement-wise, and
max(a,b) maximum(a, b) returns the
maximum value
from each pair
norm(v) sqrt(v @ v) or np.linalg.norm(v) L2 norm of vector v
element-by-element
a & b logical_and(a,b) AND operator
(NumPy ufunc) See
note LOGICOPS
element-by-element
a | b logical_or(a,b) OR operator
(NumPy ufunc) See
note LOGICOPS
bitwise AND
bitand(a,b) a & b operator (Python
native and NumPy
ufunc)
bitwise OR operator
bitor(a,b) a | b (Python native and
NumPy ufunc)
inverse of square
inv(a) linalg.inv(a)
matrix a
pseudo-inverse of
pinv(a) linalg.pinv(a)
matrix a
MATLAB NumPy Notes
matrix rank of a 2D
rank(a) linalg.matrix_rank(a)
array / matrix a
linalg.solve(a,b) if a is solution of a x = b
a\b
square; linalg.lstsq(a,b) otherwise for x
b/a solution of x a = b
Solve a.T x.T = b.T instead
for x
singular value
[U,S,V]=svd(a) U, S, Vh = linalg.svd(a), V = Vh.T
decomposition of a
cholesky
factorization of a
matrix (chol(a) in
matlab returns an
upper triangular
chol(a) linalg.cholesky(a).T
matrix,
but linalg.chole
sky(a) returns a
lower triangular
matrix)
eigenvalues and
[V,D]=eig(a) D,V = linalg.eig(a)
eigenvectors of a
eigenvalues and
[V,D]=eig(a,b) V,D = np.linalg.eig(a,b)
eigenvectors of a, b
find the k largest
[V,D]=eigs(a,k) eigenvalues and
eigenvectors of a
[Q,R,P]=qr(a,0) Q,R = scipy.linalg.qr(a) QR decomposition
LU decomposition
L,U = scipy.linalg.lu(a) or LU,P=scipy. (note: P(Matlab) ==
[L,U,P]=lu(a)
linalg.lu_factor(a) transpose(P(numpy
)) )
conjgrad scipy.sparse.linalg.cg Conjugate gradients
solver
Fourier transform
fft(a) fft(a)
of a
inverse Fourier
ifft(a) ifft(a)
transform of a
sort(a) sort(a) or a.sort() sort the matrix
[b,I] = sortrows( sort the rows of the
I = argsort(a[:,i]), b=a[I,:]
a,i) matrix
regress(y,X) linalg.lstsq(X,y) multilinear
regression
decimate(x, q) scipy.signal.resample(x, len(x)/q) downsample with
low-pass filtering
unique(a) unique(a)
squeeze(a) a.squeeze()