Skip to main content
v2026.11,858 entries · CC-BY 4.0

MATLAB Inverse Matrix: inv, Backslash and pinv

How to invert a matrix in MATLAB with inv, when backslash or pinv is the better choice, and how to check accuracy.

Written and maintained by CASRAI Editorial Board

Last updated

Short answer: in MATLAB, Y = inv(X) returns the inverse of a square matrix X, and X^(-1) is equivalent. If the reason you want the inverse is to solve Ax = b, MathWorks recommends x = A\b instead, because it is faster and more accurate. For non-square or singular matrices, use pinv.

This page is for researchers and analysts who need a matrix inverse in MATLAB and want to know which function to use, how to check the result, and what to do when it fails. Commands below follow the MathWorks documentation read on 2026-11-07.

What it is used for in research

An inverse X-1 satisfies X*X-1 = I and exists only for a square, nonsingular matrix. Researchers meet it in least-squares estimation, covariance and precision matrices, and transformations between coordinate systems. The MathWorks documentation notes that the explicit inverse is seldom necessary: most uses that look like they need inv are really linear-system solves. See the decision table below before reaching for inv.

Install or access (official source only)

MATLAB is a MathWorks product, obtained from MathWorks or through your institution’s licence. inv, pinv, rcond, cond and the backslash operator are part of core MATLAB documentation (the matlab help set), so no add-on toolbox is needed for the steps here. Free alternatives are listed further down.

Step-by-step: invert a matrix in MATLAB

  1. Enter a square matrix. The documentation example is X = [1 0 2; -1 5 0; 0 3 -9];
  2. Check that it is invertible and well scaled. Run rcond(X) or cond(X). The documentation says a badly scaled or nearly singular matrix loses accuracy in inv, and that MATLAB prints a warning in that case.
  3. Compute the inverse. Y = inv(X) (or Y = X^(-1)).
  4. Verify. Compute Y*X. Because inv uses floating-point arithmetic, the product is close to, but not exactly, the identity matrix (see the identity matrix in MATLAB, eye(n)).

Inverse of a 3×3 matrix

Using the documentation example, Y = inv(X) displays:

Y =    0.8824   -0.1176    0.1961    0.1765    0.1765    0.0392    0.0588    0.0588   -0.0980

Check one entry by hand: the determinant of this X is -51, and the (1,1) cofactor is -45, so the first entry is -45 / -51 = 0.8824, matching the output.

Inverse of a 2×2 matrix

For A = [4 7; 2 6], the determinant is 4*6 – 7*2 = 10, and the 2×2 formula gives (1/10) * [6 -7; -2 4], so inv(A) returns

    0.6000   -0.7000   -0.2000    0.4000

The same function handles any square size, so there is no separate 2×2 or 3×3 command.

“Flip” a matrix is not the same as inverting it

Searches for how to “flip” a matrix usually mean the inverse, but MATLAB’s flip function does something different: B = flip(A) returns an array the same size as A with the order of the elements reversed, and flip(A,2) reverses the elements in each row. It does not compute an inverse. For the inverse use inv; for the transpose see transpose in MATLAB.

Inverse without using inv

If you want the effect of inv(A)*b, write x = A\b. The documentation states that A\b is computed differently from inv(A)*b and is recommended for solving systems of linear equations; its own timing example shows the backslash form running faster with smaller residuals. For sparse input, the documentation says inv(X) itself computes X\speye(size(X)), that is, a backslash solve against the identity. See also solving equations in MATLAB.

Which to use: inv, backslash, pinv or lsqminnorm

Goal Use What the documentation says
You need the inverse matrix itself, square and nonsingular inv(X) Returns the inverse of a square matrix; check conditioning with rcond or cond.
Solve Ax = b A\b Recommended over inv(A)*b: faster and more accurate.
Matrix is non-square or singular pinv(A) Exists for any matrix; inv(A) does not exist for these.
Minimum-norm least-squares solution of Ax = b lsqminnorm(A,b) Recommended instead of pinv(A)*b.
Square nonsingular matrix, using pinv pinv(A) Described as “simply an expensive way to compute inv(A).”

Output and how to read it

  • Y*X near identity: expected; tiny off-diagonal values are floating-point rounding, not an error.
  • A warning from inv: the matrix is badly scaled or nearly singular. Treat the result as unreliable and inspect rcond(X) or cond(X).
  • pinv output: has the dimensions of A'. By default singular values at or below max(size(A))*eps(norm(A)) are treated as zero; pinv(A,tol) sets your own tolerance.

Troubleshooting

  • Warning about a singular or badly scaled matrix. A square matrix is singular only when its determinant is exactly zero, but near-singular matrices also lose accuracy. Rescale the data, drop collinear columns, or use pinv if you need a generalised inverse.
  • Matrix is not square. inv applies to square matrices only; use pinv.
  • Result differs between pinv(A)*b and A\b for a singular system. The documentation says they give different solutions (A\b has rank(A) nonzeros, pinv gives the minimum-norm solution). Pick the one that matches your model, or use lsqminnorm.
  • GPU arrays. The documentation says the GPU version cannot detect badly scaled or nearly singular matrices, so it prints no warning; run rcond yourself.
  • Related matrix operations. For eigenvalues see eigenvalues in MATLAB; for basics see matrices in MATLAB.

Free alternatives

Tool Inverse command Behaviour stated in its documentation
GNU Octave inv(A); [x, rcond] = inv(A) Warns for ill-conditioned matrices when rcond is not requested; recommends A \ b over inv(A) * b; pinv(A) returns the Moore-Penrose pseudoinverse.
NumPy numpy.linalg.inv(a) Raises LinAlgError if the matrix is detected singular; a nearly singular matrix may give an inaccurate result without an error, so check with numpy.linalg.cond.

NumPy users may also want reversing arrays in NumPy, which is element reversal rather than inversion.

FAQ

How do I find the inverse of a matrix in MATLAB?

Use Y = inv(X) or X^(-1) for a square, nonsingular matrix. Verify with Y*X, which should be close to the identity.

How to make a 3×3 matrix inverse?

Enter the matrix, for example X = [1 0 2; -1 5 0; 0 3 -9];, then run inv(X). The result is shown above.

How to inverse a 2×2 matrix?

The same inv call works. By hand, swap the diagonal entries, negate the off-diagonal ones and divide by the determinant (ad – bc).

How do I flip a matrix in MATLAB?

If you mean reverse its element order, use flip(A) (columns) or flip(A,2) (rows). If you mean invert it, use inv(A).

How to perform matrix inverse without inv?

To solve a system, use A\b. For a non-square or singular matrix, use pinv(A).

Sources (official documentation, read 2026-11-07)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Inverse Matrix: inv, Backslash and pinv

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →