Compare solutions to a system of linear equations obtained by backslash (
If a rectangular coefficient matrix
A is of low rank, then the least-squares problem of minimizing
norm(A*x-b) has infinitely many solutions. Two solutions are returned by
x1 = A\b and
x2 = pinv(A)*b. The distinguishing properties of these solutions are that
x1 has only
rank(A) nonzero components, and
norm(x2) is smaller than for any other solution.
Create an 8-by-6 matrix that has
rank(A) = 3.
A = magic(8); A = A(:,1:6)
A = 8×6 64 2 3 61 60 6 9 55 54 12 13 51 17 47 46 20 21 43 40 26 27 37 36 30 32 34 35 29 28 38 41 23 22 44 45 19 49 15 14 52 53 11 8 58 59 5 4 62
Create a vector for the right-hand side of the system of equations.
b = 260*ones(8,1)
b = 8×1 260 260 260 260 260 260 260 260
The number chosen for the right-hand side, 260, is the value of the 8-by-8 magic sum for
A. If A were still an 8-by-8 matrix, then one solution for x would be a vector of 1s. With only six columns, a solution exists since the equations are still consistent, but the solution is not all 1s. Since the matrix is of low rank, there are infinitely many solutions.
Solve for two of the solutions using backslash and
x1 = A\b
Warning: Rank deficient, rank = 3, tol = 1.882938e-13.
x1 = 6×1 3.0000 4.0000 0 0 1.0000 0
x2 = pinv(A)*b
x2 = 6×1 1.1538 1.4615 1.3846 1.3846 1.4615 1.1538
Both of these solutions are exact, in the sense that
norm(A*x2-b) are on the order of roundoff error. The solution
x1 is special because it has only three nonzero elements. The solution
x2 is special because
norm(x2) is smaller than it is for any other solution, including
ans = 5.0990
ans = 3.2817
A— Input matrix
Complex Number Support: Yes
tol— Singular value tolerance
Singular value tolerance, specified as a scalar.
treats singular values that are smaller than
tol as zeros
during the computation of the pseudoinverse.
The default tolerance is
The Moore-Penrose pseudoinverse is a matrix that can act as a partial replacement for the matrix inverse in cases where it does not exist. This matrix is frequently used to solve a system of linear equations when the system does not have a unique solution or has many solutions.
For any matrix
A, the pseudoinverse
exists, is unique, and has the same dimensions as
A is square and not singular, then
is simply an expensive way to compute
inv(A). However, if
A is not square, or is square and singular, then
inv(A) does not exist. In these cases,
pinv(A) has some (but not all) of the properties of
The pseudoinverse computation is based on
calculation treats singular values less than
tol as zero.
You can replace most uses of
pinv applied to a vector
b, as in
lsqminnorm(A,b) to get the minimum-norm least-squares
solution of a system of linear equations.
generally more efficient than
pinv, and it also supports
pinv uses the singular value decomposition to form the
A. Singular values along the diagonal of
S that are smaller than
tol are treated as
zeros, and the representation of
The pseudoinverse of
A is then equal to:
NaNfor nonfinite inputs
Behavior changed in R2021b
NaN values when the input
contains nonfinite values (
pinv threw an error when the input contained
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
backgroundPoolor accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).