Learn R Programming

comparator (version 0.1.3)

Minkowski: Minkowski Numeric Comparator

Description

The Minkowski distance (a.k.a. L-p distance) between two vectors \(x\) and \(y\) is the p-th root of the sum of the absolute differences of their Cartesian coordinates raised to the p-th power: $$\mathrm{Minkowski}(x,y) = \left(\sum_{i = 1}^{n} |x_i - y_i|^p\right)^{1/p}.$$

Usage

Minkowski(p = 2)

Value

A Minkowski instance is returned, which is an S4 class inheriting from NumericComparator.

Arguments

p

a positive numeric specifying the order of the distance. Defaults to 2 (Euclidean distance). If p < 1 the Minkowski distance does not satisfy the triangle inequality and is therefore not a proper distance metric.

See Also

Other numeric comparators include Manhattan, Euclidean and Chebyshev.

Examples

Run this code
## Distance between two vectors
x <- c(0, 1, 0, 1, 0)
y <- seq_len(5)
Minkowski()(x, y)

## Distance between rows (elementwise) of two matrices
comparator <- Minkowski()
x <- matrix(rnorm(25), nrow = 5)
y <- matrix(rnorm(5), nrow = 1)
elementwise(comparator, x, y)

## Distance between rows (pairwise) of two matrices
pairwise(comparator, x, y)

Run the code above in your browser using DataLab