pinv - Moore-Penrose pseudoinverse - MATLAB (original) (raw)
Main Content
Moore-Penrose pseudoinverse
Syntax
Description
B = pinv([A](#d126e1297737),[tol](#d126e1297767))
specifies a value for the tolerance. pinv
treats singular values of A
that are less than or equal to the tolerance as zero.
Examples
Solve System of Linear Equations Using Pseudoinverse
Compare solutions to a system of linear equations obtained by backslash (\
), pinv
, and lsqminnorm
.
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 side of the system of equations.
b = 8×1
260 260 260 260 260 260 260 260
The number chosen for the right 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 because the equations are still consistent, but the solution is not all 1s. Because the matrix is of low rank, there are infinitely many solutions.
Solve for two of the solutions using backslash and pinv
.
Warning: Rank deficient, rank = 3, tol = 1.882938e-13.
x1 = 6×1
3.0000
4.0000
0
0
1.0000
0
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*x1-b)
and norm(A*x2-b)
are on the order of round-off 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 norm(x1)
.
Using lsqminnorm
to compute the least-squares solution of this problem produces the same solution as using pinv
does. lsqminnorm(A,b)
is typically more efficient than pinv(A)*b
.
x3 = 6×1
1.1538
1.4615
1.3846
1.3846
1.4615
1.1538
Input Arguments
A
— Input matrix
matrix
Input matrix.
Data Types: single
| double
Complex Number Support: Yes
tol
— Singular value tolerance
scalar
Singular value tolerance, specified as a scalar. pinv
treats singular values that are less than or equal to tol
as zeros during the computation of the pseudoinverse.
The default tolerance ismax(size(A))*eps(norm(A))
.
Example: pinv(A,1e-4)
More About
Moore-Penrose Pseudoinverse
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 B
exists, is unique, and has the same dimensions as A'
. IfA
is square and not singular, then pinv(A)
is simply an expensive way to compute inv(A)
. However, ifA
is not square, or is square and singular, theninv(A)
does not exist. In these cases,pinv(A)
has some (but not all) of the properties ofinv(A)
:
Here, A B and B A are Hermitian. The pseudoinverse computation is based onsvd(A)
. The calculation treats singular values less than or equal to tol
as zero.
Tips
- You can replace most uses of
pinv
applied to a vectorb
, as inpinv(A)*b
, withlsqminnorm(A,b)
to get the minimum-norm least-squares solution of a system of linear equations. For example, in Solve System of Linear Equations Using Pseudoinverse,lsqminnorm
produces the same solution aspinv
does.lsqminnorm
is generally more efficient thanpinv
becauselsqminnorm
uses the complete orthogonal decomposition of A to find its low-rank approximation and applies its factors tob
. In contrast,pinv
uses singular value decomposition to explicitly form the pseudoinverse ofA
that you then must multiply byb
.lsqminnorm
also supports sparse matrices.
Algorithms
pinv
uses singular value decomposition to form the pseudoinverse of A
. Singular values along the diagonal of S
that are less than or equal to tol
are treated as zeros, and the representation of A
becomes:
The pseudoinverse of A
is then equal to:
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same limitations apply to GPU code generation.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The pinv
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
R2021b: pinv
returns NaN
for nonfinite inputs
pinv
returns NaN
values when the input contains nonfinite values (Inf
or NaN
). Previously, pinv
threw an error when the input contained nonfinite values.