Accelerating the pace of engineering and science

• Trials

# norm

Vector and matrix norms

## Description

example

n = norm(X) returns the 2-norm of input X and is equivalent to norm(X,2). If X is a vector, this is equal to the Euclidean distance. If X is a matrix, this is equal to 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

Create a vector corresponding to the point (-2,3,-1) in 3-D space.

`X = [-2 3 -1];`

Calculate the 2-norm of the vector.

`n = norm(X)`
```n =

3.7417```

The 2-norm is equal to the Euclidean length of the vector.

Calculate the 1-norm of the vector.

`n = norm(X,1)`
```n =

6```

The 1-norm is equal to the sum of the element magnitudes.

### 2-Norm of Matrix

Create a matrix.

`X = [2 0 1;-1 1 0;-3 3 0];`

The matrix has rank(X) = 2, so it has two nonzero singular values.

Calculate the 2-norm of the matrix.

`n = norm(X)`
```n =

4.7234```

The 2-norm is equal to the largest singular value of X.

### Frobenius Norm of Sparse Matrix

Create a sparse identity matrix, S.

`S = sparse(1:25,1:25,1);`

Attempt to use norm(S) to calculate the 2-norm of S.

`n = norm(S)`
```Error using norm
Sparse norm(S,2) is not available.```

When the input matrix is sparse, norm(S) returns an error.

Now use 'fro' to calculate the Frobenius norm of the sparse matrix.

`n = norm(S,'fro')`
```n =

5```

This calculates the 2-norm of the column vector, S(:).

## Input Arguments

expand all

### X — Numeric arrayscalar | vector | matrix

Numeric array, specified as a scalar, vector, or matrix. If X is sparse, then norm(X) returns an error.

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). The following 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.