Matrix languages · translation
MATLAB Matrix Functions and Their Equivalents in Other Languages
Moving numerical code between matrix languages mostly means translating function names, but the real trouble hides in behavior: which dimension a function works along, whether indexing starts at one or zero, and whether a star means matrix product or entrywise product. This page lists common MATLAB matrix functions with their counterparts in GNU Octave, NumPy, Julia and Scilab, and then explains the points where a straight rename produces wrong numbers. Questions such as how to take the norm of each row, or how to compare two matrices, are the usual starting points.
A correspondence table
| Task | MATLAB | Octave | NumPy | Julia | Scilab |
|---|---|---|---|---|---|
| Matrix product | A*B | A*B | A @ B | A*B | A*B |
| Entrywise product | A.*B | A.*B | A * B | A.*B | A.*B |
| Transpose | A.’ | A.’ | A.T | transpose(A) | A.’ |
| Solve Ax = b | A\b | A\b | np.linalg.solve(A, b) | A\b | A\b |
| Eigenvalues | eig(A) | eig(A) | np.linalg.eigvals(A) | eigvals(A) | spec(A) |
| Sum of each column | sum(A) | sum(A) | A.sum(axis=0) | sum(A, dims=1) | sum(A, “r”) |
| Norm of each row | vecnorm(A, 2, 2) | norm(A, 2, “rows”) | np.linalg.norm(A, axis=1) | norm.(eachrow(A)) | sqrt(sum(A.^2, “c”)) |
| Last element | A(end) | A(end) | A[-1] | A[end] | A($) |
| Identical matrices | isequal(A, B) | isequal(A, B) | np.array_equal(A, B) | A == B | isequal(A, B) |
Julia and NumPy need a package import for some entries (the LinearAlgebra standard library in Julia, NumPy itself in Python). Older MATLAB releases lack vecnorm, in which case sqrt(sum(A.^2, 2)) does the same job for real data.
Row norms and the dimension argument
Take A = [3 4; 6 8; 5 12]. The 2-norm of each row is the square root of the sum of its squared entries: √(9 + 16) = 5, √(36 + 64) = 10, √(25 + 144) = 13. Every language in the table returns the column of values 5, 10, 13 (or a plain vector of them, in NumPy), though the container type differs.
The pitfall is the default. The command norm(A) on a matrix does not give row norms; it returns the largest singular value of the whole matrix. Functions such as sum, mean and cumulative sums default to the first non-singleton dimension in MATLAB-style languages, so a row vector and a column vector can produce different shapes from the same call. NumPy asks for an axis and, if you omit it, collapses everything into one number. When translating, write the dimension argument explicitly on both sides.
Comparing matrices
Comparing two matrices entry by entry with == produces a logical matrix of the same shape. To get a single yes-or-no answer, wrap it in all(all(A == B)) in older MATLAB-style code, or use isequal. For floating-point data, exact equality is rarely what you want. Compare with a tolerance instead, for example max(abs(A − B), [], “all”) < 1e-12, or the allclose function in NumPy. Julia’s isapprox does the same with relative scaling.
NaN breaks equality: NaN == NaN is false, so isequal(A, A) is false for a matrix holding a NaN in MATLAB-style languages unless a NaN-aware variant is used.
Indexing and shapes
MATLAB, Octave, Julia and Scilab are one-based and column-major; NumPy is zero-based and, by default, row-major. A(2, 3) in the first group is A[1, 2] in NumPy. Slice ends are inclusive in MATLAB-style languages and exclusive in NumPy, so A(1:3, :) corresponds to A[0:3, :]. The correspondence is described in more detail under elements of a matrix.
Another trap is dimensionality. A MATLAB vector always has two dimensions (1 × n or n × 1), so transposing it flips it. A NumPy one-dimensional array has one dimension, and its transpose is itself. Code that relies on the flip silently stops working after translation. Reshape to an explicit column, x.reshape(-1, 1), when the orientation matters.
Operators that change meaning
The star is the biggest hazard. MATLAB-style languages use * for the matrix product and .* for the entrywise one. NumPy arrays use * for the entrywise product and @ for the matrix product. Details of the first convention are in element-by-element multiplication. Division follows the same split: a backslash in MATLAB-style languages is a linear solve, as in matrix division, whereas NumPy has no such operator and exposes the solve as a function.
Checking a translation
Translate a small, fully worked example first, such as the LU example in LU factorization, and compare the answers to several digits before porting the larger program. Factorizations can differ legitimately: eigenvectors may change sign, and an LU routine may order the pivots differently. Compare invariants, such as eigenvalues, residuals Ax − b, or the product of the factors, not the factors themselves.
For a wider view of how these languages relate, see MATLAB-style matrix languages compared. The linear algebra hub collects further reference pages.