norm - Vector and matrix norms - MATLAB (original) (raw)

Syntax

Description

[n](#bt0y64b-1-n) = norm([v](#bt0y64b-1-v)) returns the Euclidean norm of vector v. This norm is also called the 2-norm, vector magnitude, or Euclidean length.

example

[n](#bt0y64b-1-n) = norm([X](#bt0y64b-1-X)) returns the 2-norm or maximum singular value of matrix X, which is approximately max(svd(X)).

example

[n](#bt0y64b-1-n) = norm([X](#bt0y64b-1-X),[p](#bt0y64b-1-p)) returns the _p_-norm of matrix X, wherep is 1, 2, orInf:

Examples

collapse all

Create a vector and calculate the magnitude.

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

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

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

Calculate the distance between two points as the norm of the difference between the vector elements.

Create two vectors representing the (x,y) coordinates for two points on the Euclidean plane.

Use norm to calculate the distance between the points.

Geometrically, the distance between the points is equal to the magnitude of the vector that extends from one point to the other.

a=0iˆ+3jˆb=-2iˆ+1jˆd(a,b)=||b-a||=(-2-0)2+(1-3)2=8

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)

Calculate the Frobenius norm of a 4-D array X, which is equivalent to the 2-norm of the column vector X(:).

X = rand(3,4,4,3); n = norm(X,"fro")

The Frobenius norm is also useful for sparse matrices because norm(X,2) does not support sparse X.

Input Arguments

collapse all

Input vector.

Data Types: single | double
Complex Number Support: Yes

Input array, specified as a matrix or array. For most norm types,X must be a matrix. However, for Frobenius norm calculations, X can be an array.

Data Types: single | double
Complex Number Support: Yes

Norm type, specified as 2 (default), a positive real scalar, Inf, or -Inf. The valid values of p and what they return depend on whether the first input to norm is a matrix or vector, as shown in the table.

Note

This table does not reflect the actual algorithms used in calculations.

p Matrix Vector
1 max(sum(abs(X))) sum(abs(v))
2 max(svd(X)) sum(abs(v).^2)^(1/2)
Positive, real-valued numeric scalar sum(abs(v).^p)^(1/p)
Inf max(sum(abs(X'))) max(abs(v))
-Inf min(abs(v))

Output Arguments

collapse all

Norm value, returned as a scalar. The norm gives a measure of the magnitude of the elements. By convention:

More About

collapse all

The Euclidean norm (also called the vector magnitude, Euclidean length, or 2-norm) of a vector v with N elements is defined by

The general definition for the _p_-norm of a vector v that has N elements is

where p is any positive real value,Inf, or -Inf.

The maximum absolute column sum of anm-by-n matrix X (withm,n >= 2) is defined by

The maximum absolute row sum of anm-by-n matrix X (withm,n >= 2) is defined by

The Frobenius norm of anm-by-n matrix X (withm,n >= 2) is defined by

This definition also extends naturally to arrays with more than two dimensions. For example, if X is an N-D array of sizem-by-n-by-p-by-...-by-q, then the Frobenius norm is

Tips

Extended Capabilities

expand all

Thenorm function fully supports tall arrays. For more information, see Tall Arrays.

The norm 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

expand all

You can generate C/C++ code for this function.

Norm calculations of the form norm(X,"fro") support N-D array inputs.