Documentation

norm

Vector and matrix norms

Syntax

n = norm(v)
n = norm(v,p)
n =规范(X)
n = norm(X,p)
n = norm(X,'fro')

Description

example

n= norm(v)returns theEuclidean normof vectorv. This norm is also called the 2-norm, vector magnitude, or Euclidean length.

example

n= norm(v,p)returns thegeneralized vector p-norm.

example

n= norm(X)returns the 2-norm or maximum singular value of matrixX, which is approximatelymax(svd(X)).

example

n= norm(X,p)returns thep-norm of matrixX, wherepis1,2, orInf:

example

n= norm(X,'fro')returns theFrobenius normof matrixX.

Examples

艾尔崩溃l

Create a vector and calculate the magnitude.

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

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

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

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.

a = [0 3]; b = [-2 1];

Usenormto calculate the distance between the points.

d = norm(b-a)
d = 2.8284

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

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

X = [2 0 1; 1 1 0 3 3 0];n =规范(X)
n = 4.7234

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

艾尔崩溃l

Input vector.

Data Types:single|double
Complex Number Support:Yes

Input matrix.

Data Types:single|double
Complex Number Support:Yes

Norm type, specified as2(default), a different positive integer scalar,Inf, or-Inf. The valid values ofpand what they return depend on whether the first input tonormis 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(X))
2 max(svd(X)) sum(abs(X).^2)^(1/2)
Positive, real-valued numericp sum(abs(X).^p)^(1/p)
Inf max(sum(abs(X'))) max(abs(X))
-Inf min(abs(X))

Output Arguments

艾尔崩溃l

Matrix or vector norm, returned as a scalar. The norm gives a measure of the magnitude of the elements. By convention,normreturnsNaNif the input containsNaNvalues.

More About

艾尔崩溃l

Euclidean Norm

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

v = k = 1 N | v k | 2 .

General Vector Norm

The general definition for the p-norm of a vectorvthat hasNelements is

v p = [ k = 1 N | v k | p ] 1 / p ,

wherepis any positive real value,Inf, or-Inf. Some interesting values ofpare:

  • Ifp = 1, then the resulting 1-norm is the sum of the absolute values of the vector elements.

  • Ifp = 2, then the resulting 2-norm gives the vector magnitude or Euclidean length of the vector.

  • Ifp = Inf, then v = max i ( | v ( i ) | ) .

  • Ifp = -Inf, then v = min i ( | v ( i ) | ) .

Maximum Absolute Column Sum

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

X 1 = max 1 j n ( i = 1 m | a i j | ) .

Maximum Absolute Row Sum

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

X = max 1 i m ( j = 1 n | a i j | ) .

Frobenius Norm

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

X F = i = 1 m j = 1 n | a i j | 2 = trace ( X X ) .

Tips

  • Usevecnormto treat a matrix or array as a collection of vectors and calculate the norm along a specified dimension. For example,vecnormcan calculate the norm of each column in a matrix.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?