Distance between vectors and a matrix | R Documentation |

Distance between vectors and a matrix.

dista(xnew, x, type = "euclidean", k = 0, index = FALSE, trans = TRUE, square = FALSE)

`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". |

`k` |
Should the k smaller distances or their indices be returned? If k > 0 this will happen. |

`index` |
In case k is greater than 0, you have the option to get the indices of the k smallest distances. |

`trans` |
Do you want the returned matrix to be transposed? TRUE or FALSE. |

`square` |
If you choose "euclidean" as the method, then you can have the optino to return the squared Euclidean distances by setting this argument to TRUE. |

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 calculations, which can be avoided.

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.

Michail Tsagris.

R implementation and documentation: Michail Tsagris <mtsagris@uoc.gr> and Manos Papadakis <papadakm95@gmail.com>.

```
mahala, Dist, total.dist, total.dista
```

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) ) ) ) x<-b<-a<-xnew<-NULL

Rfast documentation built on Feb. 16, 2023, 5:09 p.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.