MATLAB equivalent functions in Julia, NumPy, and Eigen

There are cases when you need to switch between programming languages due to company policy, targeted hardware, personal reason, budget, going from prototype to production, etc. In this post, I collected the MATLAB equivalent functions and expressions for Julia, Python/NumPy, and CPP/Eigen.

Cheatsheet

The following table contains rough equivalents for some common expressions:

e
MATLAB Julia Python/NumPy CPP/Eigen Description
[ 1 2 3; 4 5 6 ] [[1. ,2. ,3.]; [4. ,5. ,6.]] np.array([[1. ,2. ,3.], [4. ,5. ,6.]]) MatrixXd a(2, 3);
matA << 1, 2, 3, 4, 5, 6;
Define a 2D array.
a(end) a(end) a[-1] a.tail(1)
a.tail<1>()
Access the last element in the vector.
a(2,5) a(2,5) a[1, 4] a(1, 4) Access an element in a 2D array.
a(2,:) a[2, :] a[1]
a[1, :]
a.row(1) Access a row in a 2D array.
a(:,2) a[:, 2] a[:, 1] a.col(1) Access a column in a 2D array.
a(1:5,:) a[1:5, :] a[0:5]
a[:5]
a[0:5, :]
a(seq(0, 5), all)) Access the first five rows in a 2D array.
a(end-4:end,:) a[end-4:end, :] a[-5:] a(seq(last-5, last), all)) Access the last five rows in a 2D array.
a(1:3,5:9) a[1:3,5:9] a[0:3, 4:9] a(seq(0,3), seq(4, 9)) Access a specific data block in a 2D array.
a([2,4,5],[1,3]) a[[2,4,5],[1,3]] a[np.ix_([1, 3, 4], [0, 2])] a({2, 3, 5}, {1, 3}) Access data based on specific indices.
a(1:2:end,:) a[1:2:end,:] a[ ::2,:] a(seq(1,last,2), all) Access every second rows in a 2D array.
a(3:2:21,:) a[3:2:21,:] a[2:21:2,:] a(seq(2,21,2), all) Access every second rows in a 2D array within the start and end indices.
a(end:-1:1,:)
flipud(a)
a[end:-1:1,:]
reverse(a, dims = 1)
a[::-1,:] a(seq(last, 0, fix<-1>), all)
a.colwise().reverse()
Revers row order of a 2D array.
a.' transpose(a) a.transpose()
a.T
a.transpose() Transpose of a matrix.
a' a' a.conj().transpose()
a.conj().T
a.adjoint() Conjugate transpose of a matrix.
a * b a * b a @ b a * b Matrix multiplication.
a .* b a .* b a * b a.cwiseProduct(b); Elementwise vector matrix multiplication.
a\b a\b linalg.solve(a, b) if a is square, linalg.lstsq(a, b) otherwise a.lu().solve(b)) Solution of a x = b for x.
b/a b/a Solve a.T x.T = b.T instead Solution of x a = b for x.
a./b a./b a/b a.array() / b.array()
a.cwiseQuotient(b);
Elementwise matrix division.
a.^3 a.^3 a**3 a.array().pow(3).matrix()
a.array().cube().matrix()
Elementwise exponent of a matrix.
(a > 0.5) (a .> 0.5) (a > 0.5) Determine which elements in the matrix fulfils the condition.
a(a<0.5)=0 a[a .< 0.5] .= 0 a[a < 0.5]=0 Change all the values in the matrix which fulfils the condition.
a .* (a>0.5) a .* (a .> 0.5) a * (a > 0.5) Zeros all the values in the matrix which fulfils the condition.
a(:) = 3 a[:] = 3 a[:] = 3 a.fill(3) Set all the vector/matrix elements to a constant.
b = a b = copy(a) b = a.copy() MatrixXd b(a) Return an array copy of the given object.
b = a(2,:) b = a[2,:] b = a[1, :].copy() VectorXd b(a.row(1)) Return a row copy of the given object.
b = b(:) b = a[:] b = a.flatten() VectorXd b(a.array()) Turn matrix into a vector.
[a b] [a b] np.concatenate((a,b),1)
np.hstack((a,b))
np.column_stack((a,b))
np.c_[a,b]
MatrixXd c(a.rows(), a.cols()+b.cols());
c << a, b;
Concatenate columns of two matrices.
[a; b] [a; b] np.concatenate((a,b))
np.vstack((a,b))
np.r_[a,b]
MatrixXd c(a.rows() + b.rows(), a.cols());
c << a, b;
Concatenate rows of two matrices.
1:10 1:10 np.arange(1., 11.)
np.r_[1.:11.]
np.r_[1:10:10j]
Create an increasing vector.
[1:10]' [1:10]' np.arange(1.,11.)[:, np.newaxis] Create a column vector.
zeros(3,4) zeros(3, 4) np.zeros((3, 4)) MatrixXd::Zero(3,4)
a.setZero(3,4)
Create a 2D array and initialize it with zeros.
ones(3,4) ones(3,4) np.ones((3, 4)) MatrixXd::Ones(3,4)
C.setOnes(3,4)
Create a 2D array and initialize it with ones.
eye(3) eye(3) np.eye(3) MatrixXd::Identity(3,3)
a.setIdentity(3,3)
Create an identity matrix.
diag(a) Diagonal(a) np.diag(a) a.diagonal() Returns the diagonal elements of a 2D array.
rand(3,4) rand(3,4) random.random(3, 4) MatrixXd::Random(3,4) Create a 2D array and initialize it with random number.
linspace(1,3,4) range(1, 3, length = 4) np.linspace(1,3,4) VectorXd::LinSpaced(4,1,3) Create a vector with equally spaced values.
[x, y] = meshgrid(0:8,0:5) np.mgrid[0:9.,0:6.]
np.meshgrid(r_[0:9.],r_[0:6.]
x = RowVectorXd:: LinSpaced(0,8,9) .replicate(6,1);
y = VectorXd:: LinSpaced(0,5,6) .replicate(1,9);
Creates two 2D arrays.
repmat(a, m, n) repeat(a, m, n) np.tile(a, (m, n)) a.replicate(m,n) Replicate and tile an array.
size(a, 1) size(a, 1) a.shape[0] a.rows() Returns the number of rows.
size(a, 2) size(a, 2) a.shape[1] a.cols() Returns the number of columns.
max(max(a)) maximum(a) a.max()
np.nanmax(a)
a.maxCoeff() Maximum number from a Matrix.
max(a, [], 1) maximum(a, dims = 1) a.max(0) a.colwise().maxCoeff() Column-wise maximum number.
max(a,[],2) maximum(a, dims = 2) a.max(1) a.rowwise().maxCoeff() Row-wise maximum number.
max(a,b) np.maximum(a, b) a.cwiseMax(b) Maximum numbers in two arrays.
sum(a(:)) sum(a[:]) np.sum(a[:]) a.sum() Sum of the vector elements
sum(a, 1) sum(a, dims = 1) np.sum(a, 0) a.colwise().sum() Column-wise sum.
sum(a, 2) sum(a, dims = 2) np.sum(a, 1) a.rowwise().sum() Row-wise sum.
mean(a(:)) mean(a[:]) np.mean(a[:]) a.mean() Mean from a vector/matrix.
prod(a(:)) prod(a[:]) np.prod(a[:]) a.prod() Product of the vector/matrix elements.
prod(a) prod(a) np.prod(a) a.colwise().prod() Column-wise matrix product.
prod(a, 2) prod(a, dims = 2) np.prod(a,1) a.rowwise().prod() Row-wise matrix product.
norm(v) norm(v) np.sqrt(v @ v)
np.linalg.norm(v)
v.norm() Norm of a vector.
inv(a) inv(A)
A^(-1)
linalg.inv(a) a.inverse() Inverse of a matrix.
unique(a) unique(a) np.unique(a) Unique values of a matrix.
dot(a, b) dot(a, b)
a ⋅ b
np.dot(a, b) a.dot(b) Dot product of two Matrix.
det(a) det(a) a.determinant() Matrix determinant.
trace(a) tr(a) np.trace(a) a.trace() Trace of a matrix.
sort(a) sort(a, 1) np.sort(a)
a.sort(axis = 0)
Sort a vector element.
sort(a, 2) sort(a, 2) np.sort(a, axis = 1)
a.sort(axis = 1)
[U, S, V] = svd(a) U, S, V = svd(a) U, S, Vh = linalg.svd(a), V = Vh.T x = a.svd();
x.matrixU();
x.singularValues();
x.matrixV();
Singular value decomposition.
[V, D] = eig(a) V, D = eigen(a) D, V = linalg.eig(a) EigenSolver<MatrixXd> vd(a);
vd.eigenvalues();
vd.eigenvectors();
Determines the eigenvalues and eigenvectors of a given matrix.
[Q, R, P] = qr(a,0) Q, R = qr(a) Q, R = linalg.qr(a) qr = a.qr();
Q = qr.matrixQ();
R = qr.matrixR();
Orthogonal-triangular decomposition.
[L, U, P] = lu(a) L, U, P = lu(a) P, L, U = linalg.lu(a) a.lu() LU Factorization/decomposition.

Leave a Reply