Normalizes a (matrix) vector to its Euclidean norm, or 'L2'-norm.
Arguments
- x
a numeric vector, or a matrix of vectors.
Details
The norm is defined as
$$||x|| = \sqrt{\sum(x^2)}$$
The Euclidean norm can be interpreted as the length of the connection
between zero and a point specified by the vector.
The normalized vector, then, is computed as:
$$x' = x/||x||$$
Examples
v <- rnorm(10)
normalize(v) == v/sqrt(sum(v^2))
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE