Rfast (version 1.7.3)

Distance between vectors and a matrix: Distance between vectors and a matrix

Description

Distance between vectors and a matrix.

Usage

dista(xnew, x, type = "euclidean")

Arguments

xnew
A matrix with some data or a vector.
x
A matrix with the data, where rows denotes observations (vectors) and the columns contain the variables.
type
This can be either "euclidean" or "manhattan".

Value

A matrix with the distances of each xnew from each vector of x. The number of rows of the xnew and and the number of columns of xnew are the dimensions of this matrix.

Details

The target of this function is to calculate the distances between xnew and x without having to calculate the whole distance matrix of xnew and x. The latter does extra calculaitons, which can be avoided.

See Also

mahala

Examples

Run this code
xnew <- as.matrix( iris[1:10, 1:4] )
x <- as.matrix( iris[-c(1:10), 1:4] )
a <- dista(xnew, x)
b <- as.matrix( dist( rbind(xnew, x) ) )
b <- b[ 1:10, -c(1:10) ]
sum( abs(a - b) )

## see the time
x <- matrix( rnorm(1000 * 4), ncol = 4 )
system.time( dista(xnew, x) )
system.time( as.matrix( dist( rbind(xnew, x) ) ) )

Run the code above in your browser using DataLab