Learn R Programming

rchemo (version 0.1-3)

euclsq: Matrix of distances

Description

----- Matrix (\(n, m\)) of distances between row observations of two datasets \(X\) (\(n, p\)) and Y (\(m, p\))

- euclsq: Squared Euclidean distance

- mahsq: Squared Mahalanobis distance

----- Matrix (\(n, 1\)) of distances between row observations of a dataset \(X\) (\(n, p\)) and a vector \(p\) (\(n\))

- euclsq_mu: Squared Euclidean distance

- mahsq_mu: Squared Euclidean distance

Usage

euclsq(X, Y = NULL)

euclsq_mu(X, mu)

mahsq(X, Y = NULL, Uinv = NULL)

mahsq_mu(X, mu, Uinv = NULL)

Value

A distance matrix.

Arguments

X

X-data (\(n, p\)).

Y

Data (\(m, p\)) compared to X. If NULL (default), Y is set equal to X.

mu

Vector (\(p\)) compared to X.

Uinv

For Mahalanobis distance. The inverse of a Choleski factorization matrix of the covariance matrix of X. If NULL (default), Uinv is calculated internally.

Examples

Run this code

n <- 5 ; p <- 3
X <- matrix(rnorm(n * p), ncol = p)

euclsq(X)
as.matrix(stats::dist(X)^2)
euclsq(X, X)

Y <- X[c(1, 3), ]
euclsq(X, Y)
euclsq_mu(X, Y[2, ])

i <- 3
euclsq(X, X[i, , drop = FALSE])
euclsq_mu(X, X[i, ])

S <- cov(X) * (n - 1) / n
i <- 3
mahsq(X)[i, , drop = FALSE]
stats::mahalanobis(X, X[i, ], S)

mahsq(X)
Y <- X[c(1, 3), ]
mahsq(X, Y)

Run the code above in your browser using DataLab