euc_d: Euclidean Distance Calculator

Description Usage Arguments Details Value Examples

View source: R/utils.R

Description

Calculates the distance between two points. If instead given two matrices A and B of size n x d, for n number of items of d dimensions each, it will return an n - 1 length vector with the distances between each of the items (i.e. the distance between the item A[1,] and B[1,]; between A[2,] and B[2,], and so on).

Usage

1
euc_d(x, y)

Arguments

x, y

Vector or Matrix. If vector of length d, a d-dimensional point. If matrix of size n x d, n points of d dimensions.

Details

It assumes that all dimensions have the same units or that all dimensions are normalized (mean = 0 and std = 1). Adapted from here

Value

Numeric or Vector. Distance between points. If vector, distances between the ith points in the matrices.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# distance between two points
euc_d(c(0,0), c(3,4))

# ask for multiple distances
M <- matrix(0, nrow = 3, ncol = 2)
M2 <- matrix(0, nrow = 3, ncol = 2)
for (i in 1:3){
  M[i,] <- stats::runif(2) * 5
  M2[i,] <- stats::runif(2) * 5
}
euc_d(M, M2)

lucas-castillo/SampleR documentation built on Jan. 1, 2021, 8:25 a.m.