# How to calculate the norm of a vector in Matlab and Octave

To calculate the norm of a vector in Matlab and Octave, use the function norm()

**norm(v, tn)**

The v parameter is a vector.

The tn parameter is the** type of norm**

- tn=1 is the sum of the absolute values of the elements
- tn=2 is the square root of the sum of the squares of the elements (Euclidean norm)
- tn=inf is the maximum value of the elements of the vector

If the tn parameter is not specified, the function norm() calculates the **Euclidean norm (tn = 2)** by default

$$ || \vec{v} ||_2 = \sqrt{(\vec{v},\vec{v})} $$

**What is the norm of a vector?** There are several types of norm.

The Euclidean norm of a vector (tn = 2) is the root of the sum of the squares of the elements of the vector. $$ \vec{v} = \begin{pmatrix} a_1 \\ a_2 \\ a_3 \end{pmatrix} $$ $$ || \vec{v} ||_2 = \sqrt{(\vec{v},\vec{v})} = \sqrt{a_1^2+a_2^2+a_3^2} $$ The symbol (v, v) is the term-to-term product of the elements of the vector v by itself.

The norm of type tn = 1 is the sum of the absolute values $$ || \vec{v} ||_1 = |a_1|+|a_2|+|a_3| $$ The norm of type tn = inf is the maximum value $$ || \vec{v} ||_{\infty} = max(a_1 \ , \ a_2 \ , \ a_3) $$

## Example

Define a vector v

>> v=[1;2;3]

v =

1

2

3

Calculate the Euclidean norm of a vector using the norm() function

>> norm(v)

ans = 3.7417

The result is the vector norm

$$ || \vec{v} ||_2 = \sqrt{1^2+2^2+3^2} = \sqrt{14} = 3.7417 $$

**Example 2**

Calculate the type norm = 1

>> norm(v,1)

ans = 6

The result is the sum of the absolute values

$$ || \vec{v} ||_1 = |1| \ + \ |2| \ + \ |3| \ = 6 $$

**Example 3**

Calculate the norm of type = inf

>> norm(v,inf)

ans = 3

The result is the maximum value of the vector

$$ || \vec{v} ||_{\infty} = max(1 \ , \ 2 \ , \ 3) = 3 $$

**Report us an error or send a suggestion to improve this page**