# mldivide \, mrdivide / (Matrix division)

Posted 2011.10.26 23:19
To solve the matrix equation Ax = b
x = A\b;

# mldivide \, mrdivide /-Left or right matrix division

## Syntax

mldivide(A,B)     A\B
mrdivide(B,A)     B/A

## 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 andB 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. 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, Xminimizes 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. 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.

 Note   Matrix right division and matrix left division are related by the equation B/A = (A'\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.

A = magic(3)

A =

8     1     6
3     5     7
4     9     2

b = [1;2;3]

b =

1
2
3

To solve the matrix equation Ax = b, enter

x=A\b

x =

0.0500
0.3000
0.0500

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

A*x

ans =

1.0000
2.0000
3.0000

### Example 2 — A Singular

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

Warning: Matrix is singular to working precision.

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

A = magic(5);
A(:,1) = zeros(1,5); % Set column 1 of A to zeros
b = [1;2;5;7;7];
x = A\b
Warning: Matrix is singular to working precision.

ans =

NaN
NaN
NaN
NaN
NaN

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

x = pinv(A)*b

x =

0
0.0209
0.2717
0.0808
-0.0321

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

A*x-b

ans =

-0.0603
0.6246
-0.4320
0.0141
0.0415

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

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

### Example 3

Suppose that

A = [1 0 0;1 0 0];
b = [1; 2];

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

x = A\b

returns the least squares solution

x =

1.5000
0
0

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

A*x-b

ans =

0.5000
-0.5000

## Class 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:

• If both A and B have type doubleX has type double.

• If either A or B has type single, X has type single.