# pinv

Moore-Penrose pseudoinverse

## Syntax

``B = pinv(A)``
``B = pinv(A,tol)``

## Description

example

````B = pinv(A)` returns the Moore-Penrose Pseudoinverse of matrix `A`.```
````B = pinv(A,tol)` specifies a value for the tolerance. `pinv` treats singular values of `A` that are smaller than the tolerance as zero.```

## Examples

collapse all

Compare solutions to a system of linear equations obtained by backslash (`\`) and `pinv`.

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 `pinv`.

`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*x1-b)` and `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 `norm(x1)`.

`norm(x1)`
```ans = 5.0990 ```
`norm(x2)`
```ans = 3.2817 ```

## Input Arguments

collapse all

Input matrix.

Data Types: `single` | `double`
Complex Number Support: Yes

Singular value tolerance, specified as a scalar. `pinv` treats singular values that are smaller than `tol` as zeros during the computation of the pseudoinverse.

The default tolerance is `max(size(A))*eps(norm(A))`.

Example: `pinv(A,1e-4)`

collapse all

### 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'`. If `A` is square and not singular, then `pinv(A)` 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 `inv(A)`:

`$\begin{array}{l}1.\text{\hspace{0.17em}}\text{\hspace{0.17em}}ABA=A\\ 2.\text{\hspace{0.17em}}\text{\hspace{0.17em}}BAB=B\\ 3.\text{\hspace{0.17em}}\text{\hspace{0.17em}}{\left(AB\right)}^{*}=AB\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\left(\text{AB}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{Hermitian}\right)\\ 4.\text{\hspace{0.17em}}\text{\hspace{0.17em}}{\left(BA\right)}^{*}=BA\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\left(\text{BA}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{Hermitian}\right)\end{array}$`

The pseudoinverse computation is based on `svd(A)`. The calculation treats singular values less than `tol` as zero.

## Tips

• You can replace most uses of `pinv` applied to a vector `b`, as in `pinv(A)*b`, with `lsqminnorm(A,b)` to get the minimum-norm least-squares solution of a system of linear equations. `lsqminnorm` is generally more efficient than `pinv`, and it also supports sparse matrices.

## Algorithms

`pinv` uses the singular value decomposition to form the pseudoinverse of `A`. Singular values along the diagonal of `S` that are smaller than `tol` are treated as zeros, and the representation of `A` becomes:

`$\begin{array}{l}A=US{V}^{*}=\left[{U}_{1}\text{\hspace{0.17em}}\text{\hspace{0.17em}}{U}_{2}\right]\left[\begin{array}{cc}{S}_{1}& 0\\ 0& 0\end{array}\right]{\left[{V}_{1}\text{\hspace{0.17em}}\text{\hspace{0.17em}}{V}_{2}\right]}^{*}\\ A={U}_{1}{S}_{1}{V}_{1}^{*}\text{\hspace{0.17em}}.\end{array}$`

The pseudoinverse of `A` is then equal to:

`$B={V}_{1}{S}_{1}^{-1}{U}_{1}^{*}\text{\hspace{0.17em}}.$`