How to calculate the Euclidean norm of a vector in R

The Euclidean norm of a vector represents its true length. In two dimensions it is the hypotenuse of a right triangle. It is however a useful tool regardless of the dimensions because it represents the distance from the origin to a point defined by the vector.

What Is The Euclidian Norm?

A Euclidean norm is essentially the hypotenuse of a right triangle extended to refer to more than two dimensions. It represents the absolute length of the vector by being the distance between the point represented by the vector and the origin.To calculate the Euclidean norm of a vector in R programming you make use of the norm function with the format of norm(v, “2”) where v is the name of the vector being evaluated. The “2” denotes the type of norm, which in this case defines a Euclidean norm.

How We Do This

Calculating the Euclidean norm of a vector involves taking the square of each of the values within the vector, adding them together, and then taking the square root of the results. Anyone familiar with geometry will recognize this as the hypotenuse right triangle when applied to two dimensions. As a result, The Euclidean norm can be described as a generalization of the hypotenuse to multiple dimensions.

Examples

Here we have three examples of calculating the Euclidean norm using the norm function. The first is a simple vector with values from one to ten, the second example uses a vector with more scattered values, and the final example uses a vector of random values.

> v = 1:10
> v
[1] 1 2 3 4 5 6 7 8 9 10
> n = norm(v, “2”)
> n
[1] 19.62142

The vector in this example is a straight list of integers from one to ten.

> v = c(5, 1, 4, 9, 7, 11, 22, 15, 33, 25)
> v
[1] 5 1 4 9 7 11 22 15 33 25
> n = norm(v, “2”)
> n
[1] 52.11526

The vector in this example has scattered values with no definite values or order.

> t = as.numeric(Sys.time())
> set.seed(t)
> v = as.integer(abs(rnorm(10)*10))
> v
[1] 6 2 8 2 16 8 6 13 3 10
> n = norm(v, “2”)
> n
[1] 27.23968

The vector in this example consists of random values. This shows that it will work on any vector.

Where You Can Use This

The most obvious application for calculating the Euclidean norm of a vector is calculating the hypotenuse of a right triangle. This is because it is the same calculation when being used in two dimensions. Regardless of the number of dimensions the Euclidean norm is the distance from the point defined by the vector to the origin. When looking at a vector geometrically this is a very handy calculation that goes beyond its use in a right triangle.

Calculating the Euclidean norm is a simple process. If you have had basic geometry, you already have the basic concept even if you do not recognize the name. It is an important part of calculating the absolute length of a multidimensional vector, making it a handy tool for your programming toolbox.

Scroll to top
Privacy Policy