MATLAB Function Reference Previous page   Next Page
mldivide \, mrdivide /

Left or right matrix division

Syntax

Description

mldivide(A,B) and the equivalent A\B perform matrix left division (back slash). A and B must be matrices that have the same number of rows, unless A is a scalar, in which case A\B performs element-wise division -- that is, A\B = A.\B.

If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way. If A is an n-by-n matrix and B is a column vector with n elements, or a matrix with several such columns, then X = A\B is the solution to the equation AX = B computed by Gaussian elimination with partial pivoting (see Algorithm for details). A warning message is displayed if A is badly scaled or nearly singular.

If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. In other words, X minimizes norm(A*X - B), the length of the vector AX - B. The rank k of A is determined from the QR decomposition with column pivoting (see Algorithm for details). The computed solution X has at most k nonzero elements per column. If k < n, this is usually not the same solution as x = pinv(A)*B, which returns a least squares solution.

mrdivide(B,A) and the equivalent B/A perform matrix right division (forward slash). B and A must have the same number of columns.

If A is a square matrix, B/A is roughly the same as B*inv(A). If A is an n-by-n matrix and B is a row vector with n elements, or a matrix with several such rows, then X = B/A is the solution to the equation XA = B computed by Gaussian elimination with partial pivoting. A warning message is displayed if A is badly scaled or nearly singular.

If B is an m-by-n matrix with m ~= n and A is a column vector with m components, or a matrix with several such columns, then X = B/A is the solution in the least squares sense to the under- or overdetermined system of equations XA = B.

Least Squares Solutions

If the equation Ax = b does not have a solution (and A is not a square matrix), x = A\b returns a least squares solution -- in other words, a solution that minimizes the length of the vector Ax - b, which is equal to norm(A*x - b). See Example 3 for an example of this.

Examples

Example 1

Suppose that A and b are the following.

To solve the matrix equation Ax = b, enter

You can verify that x is the solution to the equation as follows.

Example 2 -- A Singular

If A is singular, A\b returns the following warning.

In this case, Ax = b might not have a solution. For example,

If you get this warning, you can still attempt to solve Ax = b using the pseudoinverse function pinv.

The result x is least squares solution to Ax = b. To determine whether x is a exact solution -- that is, a solution for which Ax - b = 0 -- simply compute

The answer is not the zero vector, so x is not an exact solution.

Pseudoinverses, in the online MATLAB documentation, provides more examples of solving linear systems using pinv.

Example 3

Suppose that

Note that Ax = b cannot have a solution, because A*x has equal entries for any x. Entering

returns the least squares solution

along with a warning that A is rank deficient. Note that x is not an exact solution:

Data Type Support

When computing X = A\B or X = A/B, the matrices A and B can have data type double or single. The following rules determine the data type of the result:

Algorithm

The specific algorithm used for solving the simultaneous linear equations denoted by X = A\B and X = B/A depends upon the structure of the coefficient matrix A. To determine the structure of A and select the appropriate algorithm, MATLAB follows this precedence:

  1. If A is sparse and diagonal, X is computed by dividing by the diagonal elements of A.
  2. If A is sparse, square, and banded, then banded solvers are used. Band density is (# nonzeros in the band)/(# nonzeros in a full band). Band density = 1.0 if there are no zeros on any of the three diagonals.
  3. If A is an upper or lower triangular matrix, then X is computed quickly with a backsubstitution algorithm for upper triangular matrices, or a forward substitution algorithm for lower triangular matrices. The check for triangularity is done for full matrices by testing for zero elements and for sparse matrices by accessing the sparse data structure.
  1. If A is a full matrix, computations are performed using the Basic Linear Algebra Subprograms (BLAS) routines in the following table.


    Real
    Complex
    A and B double
    DTRSV, DTRSM
    ZTRSV, ZTRSM
    A or B single
    STRSV, STRSM
    CTRSV, CTRSM

  1. If A is a permutation of a triangular matrix, then X is computed with a permuted backsubstitution algorithm.
  2. If A is symmetric, or Hermitian, and has real positive diagonal elements, then a Cholesky factorization is attempted (see chol). If A is found to be positive definite, the Cholesky factorization attempt is successful and requires less than half the time of a general factorization. Nonpositive definite matrices are usually detected almost immediately, so this check also requires little time.
  1. If successful, the Cholesky factorization for full A is

    where R is upper triangular. The solution X is computed by solving two triangular systems,

    Computations are performed using the LAPACK routines in the following table.


    Real
    Complex
    A and B double
    DLANGE, DPOTRF, DPOTRS, DPOCON
    ZLANGE, ZPOTRF, ZPOTRS, ZPOCON
    A or B single
    SLANGE, SPOTRF, SPOTRS, SDPOCON
    CLANGE, CPOTRF, CPOTRS, CPOCON

    If A is sparse, a symmetric minimum degree preordering is applied first (see symmmd and spparms) before X is computed. The algorithm is

  1. If A is Hessenberg, but not sparse, it is reduced to an upper triangular matrix and that system is solved via substitution.
  2. If A is square and does not satisfy criteria 1 through 6, then a general triangular factorization is computed by Gaussian elimination with partial pivoting (see lu). This results in
  1. where L is a permutation of a lower triangular matrix and U is an upper triangular matrix. Then X is computed by solving two permuted triangular systems.

    If A is not sparse, computations are performed using the LAPACK routines in the following table.


    Real
    Complex
    A and B double
    DLANGE, DGESV, DGECON
    ZLANGE, ZGESV, ZGECON
    A or B single
    SLANGE, SGESV, SGECON
    CLANGE, CGESV, CGECON

    If A is sparse, then UMFPACK is used to compute X. The computations result in

    where

    Then X = Q*(U\L\(P*B)).

  1. If A is not square, then Householder reflections are used to compute an orthogonal-triangular factorization.
  1. where P is a permutation, Q is orthogonal and R is upper triangular (see qr). The least squares solution X is computed with

    If A is sparse, MATLAB computes a least squares solution using the sparse qr factorization of A.

    If A is full, MATLAB uses the LAPACK routines listed in the following table to compute these matrix factorizations.


    Real
    Complex
    A and B double
    DGEQP3, DORMQR, DTRTRS
    ZGEQP3, ZORMQR, ZTRTRS
    A or B single
    SGEQP3, SORMQR, STRTRS
    CGEQP3, CORMQR, CTRTRS

See Also

Arithmetic operators, linsolve, ldivide, rdivide


Previous page  mkpp mlint Next page

© 1994-2005 The MathWorks, Inc.