# norm

Vector and matrix norms

## Syntax

``n = norm(v)``
``n = norm(v,p)``
``n = norm(X)``
``n = norm(X,p)``
``n = norm(X,'fro')``

## Description

example

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

example

````n = norm(v,p)` returns the generalized vector p-norm.```

example

````n = norm(X)` returns the 2-norm or maximum singular value of matrix `X`, which is approximately `max(svd(X))`.```

example

````n = norm(X,p)` returns the p-norm of matrix `X`, where `p` is `1`, `2`, or `Inf`: If `p = 1`, then `n` is the maximum absolute column sum of the matrix.If `p = 2`, then `n` is approximately `max(svd(X))`. This is equivalent to `norm(X)`.If `p = Inf`, then `n` is the maximum absolute row sum of the matrix. ```

example

````n = norm(X,'fro')` returns the Frobenius norm of matrix `X`.```

## Examples

collapse all

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];```

Use `norm` to 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.

`$\begin{array}{l}a=0\underset{}{\overset{ˆ}{i}}+3\underset{}{\overset{ˆ}{j}}\\ b=-2\underset{}{\overset{ˆ}{i}}+1\underset{}{\overset{ˆ}{j}}\\ \\ \begin{array}{rl}{d}_{\left(a,b\right)}& =||b-a||\\ & =\sqrt{\left(-2-0{\right)}^{2}+\left(1-3{\right)}^{2}}\\ & =\sqrt{8}\end{array}\end{array}$`

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 ```

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

collapse all

Input vector.

Data Types: `single` | `double`
Complex Number Support: Yes

Input matrix.

Data Types: `single` | `double`
Complex Number Support: Yes

Norm type, specified as `2` (default), a different positive integer 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.

pMatrixVector
`1``max(sum(abs(X)))``sum(abs(X))`
`2` `max(svd(X))``sum(abs(X).^2)^(1/2)`
Positive, real-valued numeric `p``sum(abs(X).^p)^(1/p)`
`Inf``max(sum(abs(X')))``max(abs(X))`
`-Inf``min(abs(X))`

## Output Arguments

collapse all

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.

collapse all

### Euclidean Norm

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

`$‖v‖=\sqrt{\sum _{k=1}^{N}{|{v}_{k}|}^{2}}\text{\hspace{0.17em}}.$`

### General Vector Norm

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

`${‖v‖}_{p}={\left[\sum _{k=1}^{N}{|{v}_{k}|}^{p}\right]}^{\text{\hspace{0.17em}}1/p}\text{\hspace{0.17em}},$`

where `p` is any positive real value, `Inf`, or `-Inf`. Some interesting values of `p` are:

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

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

• If `p = Inf`, then ${‖v‖}_{\infty }={\mathrm{max}}_{i}\left(|v\left(i\right)|\right)$.

• If `p = -Inf`, then ${‖v‖}_{-\infty }={\mathrm{min}}_{i}\left(|v\left(i\right)|\right)$.

### Maximum Absolute Column Sum

The maximum absolute column sum of an `m`-by-`n` matrix `X` (with `m,n >= 2`) is defined by

`${‖X‖}_{1}=\underset{1\le j\le n}{\mathrm{max}}\left(\sum _{i=1}^{m}|{a}_{ij}|\right).$`

### Maximum Absolute Row Sum

The maximum absolute row sum of an `m`-by-`n` matrix `X` (with `m,n >= 2`) is defined by

`${‖X‖}_{\infty }=\underset{1\le i\le m}{\mathrm{max}}\left(\sum _{j=1}^{n}|{a}_{ij}|\right)\text{\hspace{0.17em}}.$`

### Frobenius Norm

The Frobenius norm of an `m`-by-`n` matrix `X` (with `m,n >= 2`) is defined by

`${‖X‖}_{F}=\sqrt{\sum _{i=1}^{m}\sum _{j=1}^{n}{|{a}_{ij}|}^{2}}=\sqrt{\text{trace}\left({X}^{†}X\right)}\text{\hspace{0.17em}}.$`

## Tips

• Use `vecnorm` to treat a matrix or array as a collection of vectors and calculate the norm along a specified dimension. For example, `vecnorm` can calculate the norm of each column in a matrix.