norm

Vector and matrix norms

Syntax

Description

example

n = norm(X) returns the 2-norm of input X and is equivalent to norm(X,2). If X is a vector, norm(X) is the Euclidean distance. If X is a matrix, norm(X) is the largest singular value of X.

example

n = norm(X,p) returns the p-norm of input X.

Examples

expand all

1- and 2- Norm of Vector

Calculate the 2-norm of a vector corresponding to the point (-2,3,-1) in 3-D space. The 2-norm is equal to the Euclidean length of the vector.

X = [-2 3 -1];
n = norm(X)
n =

    3.7417

Calculate the 1-norm of the vector, which is the sum of the element magnitudes.

n = norm(X,1)
n =

    6

2-Norm of Matrix

Calculate the 2-norm of a matrix, which is the largest singular value.

X = [2 0 1;-1 1 0;-3 3 0];
n = norm(X)
n =

    4.7234

Frobenius Norm of Sparse Matrix

Use 'fro' to calculate the Frobenius norm of a sparse matrix, which calculates the 2-norm of the column vector, S(:).

S = sparse(1:25,1:25,1);
n = norm(S,'fro')
n =

     5

Input Arguments

expand all

X — Numeric arrayscalar | vector | matrix

Numeric array, specified as a scalar, vector, or matrix. Use norm(X,'fro') when X is sparse.

Data Types: single | double
Complex Number Support: Yes

p — Norm type2 (default) | positive integer scalar | Inf | -Inf | 'fro'

Norm type, specified as 2 (default), a positive integer scalar, Inf, -Inf, or 'fro'. Whether X is a matrix or vector determines the allowed values of p (and what they return). This table lists the calculated values for each allowed value of p.

    Note:   The table does not reflect the actual algorithms used in calculations.

pMatrixVector
1max(sum(abs(X)))sum(abs(X))
2max(svd(X))sum(abs(X).^2)^(1/2)

positive, real-valued numeric p

sum(abs(X).^p)^(1/p)
Infmax(sum(abs(X')))max(abs(X))
-Infmin(abs(X))
'fro'sqrt(sum(diag(X'*X)))norm(X)

Output Arguments

expand all

n — Matrix or vector normscalar

Matrix or vector norm, returned as a scalar. The norm gives a measure of the magnitude of the elements. By convention, norm returns NaN if the input contains NaN values.

See Also

| | | |

Was this topic helpful?