# svd

Fixed-point singular value decomposition

## Syntax

``S = svd(A)``
``[U,S,V] = svd(A)``
``[U,S,V] = svd(A,0)``
``[U,S,V] = svd(A,'econ')``
``[___] = svd(___,sigmaForm)``

## Description

example

````S = svd(A)` returns the singular values of matrix `A` in descending order.```

example

````[U,S,V] = svd(A)` performs a singular value decomposition of matrix `A`, such that `A = U*S*V'`. `S` is a diagonal matrix of the same dimension as `A` with nonnegative diagonal elements in decreasing order. `U` and `V` are unitary matrices.```

example

````[U,S,V] = svd(A,0)` produces an economy-size decomposition of `A`. If `A` is an m-by-n matrix, then:m > n — Only the first n columns of `U` are computed, and `S` is n-by-n.m <= n — `svd(A,0)` is equivalent to `svd(A)`.```

example

````[U,S,V] = svd(A,'econ')` produces a different economy-size decomposition of `A`. If `A` is an m-by-n matrix, then:m >= n — `svd(A,'econ')` is equivalent to `svd(A,0)`.m < n — Only the first m columns of `V` are computed, and `S` is m-by-m.```

example

````[___] = svd(___,sigmaForm)` optionally specifies the output format for the singular values. You can use this option with any of the previous input or output combinations. Specify `'vector'` to return the singular values as a column vector. Specify `'matrix'` to return the singular values in a diagonal matrix.```

## Examples

collapse all

Compute the singular values of a full rank fixed-point matrix.

`A = fi([1 0 1; -1 -2 0; 0 1 -1])`
```A = 1 0 1 -1 -2 0 0 1 -1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 14```

Compute the singular values.

`s = svd(A)`
```s = 2.4606 1.6995 0.2391 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16```

The singular values are returned in a column vector in decreasing order.

Find the singular value decomposition of the rectangular fixed-point matrix `A`.

Define the rectangular matrix `A`.

```m = 4; n = 2; A = fi(10*randn(m,n))```
```A = 5.5278 6.6016 10.3911 -0.6787 -11.1763 -1.9521 12.6064 -2.1763 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11```

Find the singular value decomposition of the fixed-point matrix `A`.

`[U,S,V] = svd(A)`
```U = 0.2885 0.8724 -0.2790 0.2789 0.5016 -0.1879 -0.6306 -0.5617 -0.5478 -0.1725 -0.7243 0.3816 0.6043 -0.4169 0 0.6790 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30 S = 20.5887 0 0 7.1458 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 V = 0.9979 -0.0640 0.0640 0.9979 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30```

Confirm the relation `A = U*S*V'`.

`U*S*V'`
```ans = 5.5279 6.6016 10.3912 -0.6788 -11.1763 -1.9521 12.6065 -2.1764 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 99 FractionLength: 76```

Calculate the complete and economy-size decomposition of a rectangular fixed-point matrix.

Define the fixed-point matrix `A`.

```m = 5; n = 3; A = fi(10*randn(m,n));```
```A = 15.3262 -10.8906 0.8594 -7.6963 0.3252 -14.9160 3.7139 5.5254 -7.4229 -2.2559 11.0059 -10.6162 11.1738 15.4424 23.5049 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 10```

Compute the complete decomposition.

`[U,S,V] = svd(A)`
```U = 0.1418 -0.7180 0.6097 0.2442 0.1816 -0.4692 0.1854 0.0942 -0.0545 0.8565 -0.0998 0.1822 0.5982 -0.7456 -0.2074 -0.2180 0.5203 0.4894 0.6176 -0.2466 0.8380 0.3824 0.1481 0 0.3600 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30 S = 34.5589 0 0 0 22.3047 0 0 0 14.0854 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 V = 0.4418 -0.3881 0.8088 0.2400 0.9199 0.3103 0.8644 -0.0570 -0.4996 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30```

Compute the economy-size decomposition.

`[U,S,V] = fixed.svd(A,'econ')`
```U = 0.6482 0.5096 0.5333 0.4510 0.2666 -0.7651 -0.0660 -0.1794 0.3477 0.5576 -0.5920 0.0698 -0.2473 0.5353 0.0671 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30 S = 27.5262 0 0 0 16.7103 0 0 0 12.5909 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 V = 0.2309 -0.6075 -0.7600 -0.5933 -0.7070 0.3849 0.7711 -0.3620 0.5237 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30```

Use the expected result `A = U*S*V'` to determine the relative error of the calculation.

`relativeError = norm(double(U*S*V'-A))/norm(double(A))`
```relativeError = 1.3377e-05```

Create a 3-by-3 magic square matrix and calculate the singular value decomposition. By default, the `fixed.svd` function returns the singular values in a diagonal matrix when you specify multiple outputs.

Define the matrix `A`.

```m = 3; n = m; A = fi(magic(m))```
```A = 8 1 6 3 5 7 4 9 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11```

Compute the singular value decomposition.

`[U,S,V] = svd(A)`
```U = 0.5774 -0.7071 -0.4082 0.5773 0.0000 0.8165 0.5773 0.7071 -0.4083 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30 S = 15.0000 0 0 0 6.9282 0 0 0 3.4640 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 V = 0.5774 -0.4083 -0.7071 0.5773 0.8165 -0.0000 0.5773 -0.4082 0.7071 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30```

Specify the `'vector'` option to return the singular values in a column vector.

`[U,S,V] = svd(A,'vector')`
```U = 0.5774 -0.7071 -0.4082 0.5773 0.0000 0.8165 0.5773 0.7071 -0.4083 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30 S = 15.0000 6.9282 3.4640 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 V = 0.5774 -0.4083 -0.7071 0.5773 0.8165 -0.0000 0.5773 -0.4082 0.7071 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 30```

If you specify one output argument, such as `S = svd(A)`, then `svd` switches behavior to return the singular values in a column vector by default. In that case, you can specify the `'matrix'` option to return the singular values as a diagonal matrix.

## Input Arguments

collapse all

Input matrix, specified as a matrix. `A` can be a fixed-point or scaled double `fi` data type.

Data Types: `fi`
Complex Number Support: Yes

Output format of singular values, specified as one of these values:

• `'vector'``S` is a column vector. This behavior is the default when you specify one output, ```S = svd(A)```.

• `'matrix'``S` is a diagonal matrix. This behavior is the default when you specify multiple outputs, ```[U,S,V] = svd(A)```.

Example: `[U,S,V] = svd(X,'vector')` returns `S` as a column vector instead of a diagonal matrix.

Example: `S = svd(X,'matrix')` returns `S` as a diagonal matrix instead of a column vector.

Data Types: `char` | `string`

## Output Arguments

collapse all

Left singular vectors, returned as the columns of a matrix.

The fixed-point data type is adjusted to avoid overflow and increase precision. For more information, see Algorithms.

Singular values, returned as a diagonal matrix or column vector. The singular values are nonnegative and returned in decreasing order.

The fixed-point data type is adjusted to avoid overflow and increase precision. For more information, see Algorithms.

Right singular vectors, returned as the columns of a matrix.

The fixed-point data type is adjusted to avoid overflow and increase precision. For more information, see Algorithms.

## Tips

To have full control over the fixed-point types, use the `fixed.svd` function.

## Algorithms

The `svd` function adjusts the data type of a fixed-point input to avoid overflow and increase precision. The fraction length of the singular vectors `S` is adjusted to a minimum of `16`, and the word length is increased to avoid overflow with a minimum of `32`. The word length of the left and right singular vectors `U` and `V` are the same as the word length of `S`. The fraction length of `U` and `V` is two less than the word length.

## Version History

Introduced in R2022b