svd
Singular value decomposition
Description
returns
the singular values
of matrix S
= svd(A
)A
in descending order.
[___]
= svd(
produces an economy-size decomposition of A
,"econ")A
using either of the
previous output argument combinations. If A
is an
m
-by-n
matrix, then:
m > n
— Only the firstn
columns ofU
are computed, andS
isn
-by-n
.m = n
—svd(A,"econ")
is equivalent tosvd(A)
.m < n
— Only the firstm
columns ofV
are computed, andS
ism
-by-m
.
The economy-size decomposition removes extra rows or columns of zeros from the
diagonal matrix of singular values, S
, along with the columns in
either U
or V
that multiply those zeros in the
expression A = U*S*V'
. Removing these zeros and columns can
improve execution time and reduce storage requirements without compromising the
accuracy of the decomposition.
[___] = svd(
produces a different economy-size decomposition of
A
,0)m
-by-n
matrix A
:
m > n
—svd(A,0)
is equivalent tosvd(A,"econ")
.m <= n
—svd(A,0)
is equivalent tosvd(A)
.
The use of this syntax is not recommended. Use the "econ"
option instead.
[___] = svd(___,
optionally specifies the output format for the singular values. You can use this
option with any of the previous input or output argument combinations. Specify
outputForm
)"vector"
to return the singular values as a column vector, or
"matrix"
to return the singular values in a diagonal
matrix.
Examples
Singular Values of Matrix
Compute the singular values of a full rank matrix.
A = [1 0 1; -1 -2 0; 0 1 -1]
A = 3×3
1 0 1
-1 -2 0
0 1 -1
s = svd(A)
s = 3×1
2.4605
1.6996
0.2391
Singular Value Decomposition
Find the singular value decomposition of a rectangular matrix A
.
A = [1 2; 3 4; 5 6; 7 8]
A = 4×2
1 2
3 4
5 6
7 8
[U,S,V] = svd(A)
U = 4×4
-0.1525 -0.8226 -0.3945 -0.3800
-0.3499 -0.4214 0.2428 0.8007
-0.5474 -0.0201 0.6979 -0.4614
-0.7448 0.3812 -0.5462 0.0407
S = 4×2
14.2691 0
0 0.6268
0 0
0 0
V = 2×2
-0.6414 0.7672
-0.7672 -0.6414
Confirm the relation A = U*S*V'
, within machine precision.
U*S*V'
ans = 4×2
1.0000 2.0000
3.0000 4.0000
5.0000 6.0000
7.0000 8.0000
Economy-Size Decomposition
Calculate the complete and economy-size decompositions of a rectangular matrix.
A = [1 2; 3 4; 5 6; 7 8]
A = 4×2
1 2
3 4
5 6
7 8
[U,S,V] = svd(A)
U = 4×4
-0.1525 -0.8226 -0.3945 -0.3800
-0.3499 -0.4214 0.2428 0.8007
-0.5474 -0.0201 0.6979 -0.4614
-0.7448 0.3812 -0.5462 0.0407
S = 4×2
14.2691 0
0 0.6268
0 0
0 0
V = 2×2
-0.6414 0.7672
-0.7672 -0.6414
[U,S,V] = svd(A,"econ")
U = 4×2
-0.1525 -0.8226
-0.3499 -0.4214
-0.5474 -0.0201
-0.7448 0.3812
S = 2×2
14.2691 0
0 0.6268
V = 2×2
-0.6414 0.7672
-0.7672 -0.6414
Since A
is 4-by-2, svd(A,"econ")
returns fewer columns in U
and fewer rows in S
compared to a complete decomposition. Extra rows of zeros in S
are excluded, along with the corresponding columns in U
that would multiply with those zeros in the expression A = U*S*V'
.
Control Singular Value Output Format
Create a 6-by-6 magic square matrix and calculate the SVD. By default, svd
returns the singular values in a diagonal matrix when you specify multiple outputs.
A = magic(6); [U,S,V] = svd(A)
U = 6×6
-0.4082 0.5574 0.0456 -0.4182 0.3092 0.5000
-0.4082 -0.2312 0.6301 -0.2571 -0.5627 0.0000
-0.4082 0.4362 0.2696 0.5391 0.1725 -0.5000
-0.4082 -0.3954 -0.2422 -0.4590 0.3971 -0.5000
-0.4082 0.1496 -0.6849 0.0969 -0.5766 0.0000
-0.4082 -0.5166 -0.0182 0.4983 0.2604 0.5000
S = 6×6
111.0000 0 0 0 0 0
0 50.6802 0 0 0 0
0 0 34.3839 0 0 0
0 0 0 10.1449 0 0
0 0 0 0 5.5985 0
0 0 0 0 0 0.0000
V = 6×6
-0.4082 0.6234 -0.3116 0.2495 0.2511 0.4714
-0.4082 -0.6282 0.3425 0.1753 0.2617 0.4714
-0.4082 -0.4014 -0.7732 -0.0621 -0.1225 -0.2357
-0.4082 0.1498 0.2262 -0.4510 0.5780 -0.4714
-0.4082 0.1163 0.2996 0.6340 -0.3255 -0.4714
-0.4082 0.1401 0.2166 -0.5457 -0.6430 0.2357
Specify the "vector"
option to return the singular values in a column vector.
[U,S,V] = svd(A,"vector")
U = 6×6
-0.4082 0.5574 0.0456 -0.4182 0.3092 0.5000
-0.4082 -0.2312 0.6301 -0.2571 -0.5627 0.0000
-0.4082 0.4362 0.2696 0.5391 0.1725 -0.5000
-0.4082 -0.3954 -0.2422 -0.4590 0.3971 -0.5000
-0.4082 0.1496 -0.6849 0.0969 -0.5766 0.0000
-0.4082 -0.5166 -0.0182 0.4983 0.2604 0.5000
S = 6×1
111.0000
50.6802
34.3839
10.1449
5.5985
0.0000
V = 6×6
-0.4082 0.6234 -0.3116 0.2495 0.2511 0.4714
-0.4082 -0.6282 0.3425 0.1753 0.2617 0.4714
-0.4082 -0.4014 -0.7732 -0.0621 -0.1225 -0.2357
-0.4082 0.1498 0.2262 -0.4510 0.5780 -0.4714
-0.4082 0.1163 0.2996 0.6340 -0.3255 -0.4714
-0.4082 0.1401 0.2166 -0.5457 -0.6430 0.2357
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.
Rank, Column Space, and Null Space of Matrix
Use the results of the singular value decomposition to determine the rank, column space, and null space of a matrix.
A = [2 0 2; 0 1 0; 0 0 0]
A = 3×3
2 0 2
0 1 0
0 0 0
[U,S,V] = svd(A)
U = 3×3
1 0 0
0 1 0
0 0 1
S = 3×3
2.8284 0 0
0 1.0000 0
0 0 0
V = 3×3
0.7071 0 0.7071
0 1.0000 0
0.7071 0 -0.7071
Calculate the rank using the number of nonzero singular values.
s = diag(S); rank_A = nnz(s)
rank_A = 2
Compute an orthonormal basis for the column space of A
using the columns of U
that correspond to nonzero singular values.
column_basis = U(:,logical(s))
column_basis = 3×2
1 0
0 1
0 0
Compute an orthonormal basis for the null space of A
using the columns of V
that correspond to singular values equal to zero.
null_basis = V(:,~s)
null_basis = 3×1
0.7071
0
-0.7071
The functions rank
, orth
, and null
provide convenient ways to calculate these quantities.
Input Arguments
A
— Input matrix
matrix
Input matrix. A
can be either square or rectangular
in size.
Data Types: single
| double
Complex Number Support: Yes
outputForm
— Output format of singular values
"vector"
| "matrix"
Output format of singular values, specified as one of these values:
"vector"
—S
is a column vector. This is the default behavior when you specify one output,S = svd(X)
."matrix"
—S
is a diagonal matrix. This is the default behavior when you specify multiple outputs,[U,S,V] = svd(X)
.
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
U
— Left singular vectors
matrix
Left singular vectors, returned as the columns of a matrix.
For an
m
-by-n
matrixA
withm > n
, the economy-sized decompositionsvd(A,"econ")
computes only the firstn
columns ofU
. In this case, the columns ofU
are orthogonal andU
is anm
-by-n
matrix that satisfies .For complete decompositions,
svd(A)
returnsU
as anm
-by-m
unitary matrix satisfying . The columns ofU
that correspond to nonzero singular values form a set of orthonormal basis vectors for the range ofA
.
Different machines and releases of MATLAB® can produce different singular vectors that are still numerically accurate.
Corresponding columns in U
and V
can flip their signs,
since this does not affect the value of the expression A = U*S*V'
.
S
— Singular values
diagonal matrix | column vector
Singular values, returned as a diagonal matrix or column vector. The singular values are nonnegative and returned in decreasing order.
If A
is an m
-by-n
matrix, and S
is a diagonal matrix, then the size of
S
is as follows:
The economy-sized decomposition
svd(A,"econ")
returnsS
as a square matrix of ordermin([m,n])
.For complete decompositions,
svd(A)
returnsS
with the same size asA
.
Additionally, the singular values in S
are returned in a
column vector or diagonal matrix depending on how you call
svd
and whether you specify the
outputForm
option:
If you call
svd
with one output or specify the"vector"
option, thenS
is a column vector.If you call
svd
with multiple outputs or specify the"matrix"
option, thenS
is a diagonal matrix.
Depending on whether you specify one output or multiple outputs,
svd
can return different singular values that are
still numerically accurate.
V
— Right singular vectors
matrix
Right singular vectors, returned as the columns of a matrix.
For an
m
-by-n
matrixA
withm < n
, the economy decompositionsvd(A,"econ")
computes only the firstm
columns ofV
. In this case, the columns ofV
are orthogonal andV
is ann
-by-m
matrix that satisfies .For complete decompositions,
svd(A)
returnsV
as ann
-by-n
unitary matrix satisfying . The columns ofV
that do not correspond to nonzero singular values form a set of orthonormal basis vectors for the null space ofA
.
Different machines and releases of MATLAB can produce different singular vectors that are still numerically accurate.
Corresponding columns in U
and V
can flip their signs,
since this does not affect the value of the expression A = U*S*V'
.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
svd
function supports tall arrays with the following usage
notes and limitations:
The three-output syntax
[U,S,V] = svd(X)
is not supported. For three outputs, you must specifysvd(X,"econ")
, and can optionally specify the"vector"
or"matrix"
options.With one output
s = svd(X,...)
, the singular values must be returned as a vector unless you specify"econ"
.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation uses a different
SVD
implementation than MATLAB uses. Because the singular value decomposition is not unique, left and right singular vectors might differ from those computed by MATLAB.Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Code generation uses a different
SVD
implementation than MATLAB uses. Because the singular value decomposition is not unique, left and right singular vectors might differ from those computed by MATLAB.When the input matrix contains a nonfinite value, the generated code does not issue an error. Instead, the output contains
NaN
values.Code generation does not support sparse matrix inputs for this function.
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 svd
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
If the input matrix
A
is rectangular, then you must specify economy-size decomposition using the"econ"
option.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aR2022b: "0" syntax not recommended for economy-size decompositions
The syntax [___] = svd(A,0)
will continue to be supported, but
is no longer recommended. Use the "econ"
option to perform
economy-size decompositions instead.
R2021b: Specify output format
Specify outputForm
as "vector"
or
"matrix"
to control whether svd
returns
the output arguments as vectors or matrices. For large decompositions, returning the
outputs as vectors can save memory and improve efficiency.
R2021b: svd
returns NaN
for nonfinite inputs
svd
returns NaN
values when the input
contains nonfinite values (Inf
or NaN
).
Previously, svd
threw an error when the input contained
nonfinite values.
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)