MATLAB To Python
MATLAB To Python
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
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
construct a matrix
[ a b; c d ] block([[a,b], [c,d]]) from blocks a, b, c,
and d
access element in
a(2,5) a[1,4] second row, fifth
column
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
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)
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
4 equally spaced
linspace(1,3,4) linspace(1,3,4) samples between 1
and 3, inclusive
[x,y]=meshgrid([1
meshgrid([1,2,4],[2,4,5])
,2,4],[2,4,5])
create m by n
repmat(a, m, n) tile(a, (m, n))
copies of a
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
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
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
)) )
Fourier transform
fft(a) fft(a)
of a
inverse Fourier
ifft(a) ifft(a)
transform of a
squeeze(a) a.squeeze()