Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate all pairwise distances in two dimensions

Say I have data concerning the position of animals on a 2d plane (as determined by video monitoring from a camera directly overhead). For example a matrix with 15 rows (1 for each animal) and 2 columns (x position and y position)

animal.ids<-letters[1:15]  
xpos<-runif(15) # x coordinates 
ypos<-runif(15) # y coordinates 
raw.data.t1<-data.frame(xpos, ypos)
  rownames(raw.data.t1) = animal.ids

I want to calculate all the pairwise distances between animals. That is, get the distance from animal a (row 1) to the animal in row 2, row3...row15, and then repeat that step for all rows, avoiding redundant distance calculations. The desire output of a function that does this would be the mean of all the pairwise distances. I should clarify that I mean the simple linear distance, from the formula d<-sqrt(((x1-x2)^2)+((y1-y2)^2)). Any help would be greatly appreciated.

Furthermore, how could this be extended to a similar matrix with an arbitrarily large even number of columns (every two columns representing x and y positions at a given time point). The goal here would be to calculate mean pairwise distances for every two columns and output a table with each time point and its corresponding mean pairwise distance. Here is an example of the data structure with 3 time points:

xpos1<-runif(15) 
ypos1<-runif(15) 
xpos2<-runif(15) 
ypos2<-runif(15)
xpos3<-runif(15) 
ypos3<-runif(15)
pos.data<-cbind(xpos1, ypos1, xpos2, ypos2, xpos3, ypos3)
    rownames(pos.data) = letters[1:15]
like image 339
distance deprived Avatar asked Mar 30 '11 15:03

distance deprived


People also ask

How do you find the pairwise distance?

Description. D = pdist( X ) returns the Euclidean distance between pairs of observations in X . D = pdist( X , Distance ) returns the distance by using the method specified by Distance .

What is pairwise distance matrix?

In mathematics, computer science and especially graph theory, a distance matrix is a square matrix (two-dimensional array) containing the distances, taken pairwise, between the elements of a set. Depending upon the application involved, the distance being used to define this matrix may or may not be a metric.

How do you find the Euclidean distance between two vectors?

Euclidean distance is calculated as the square root of the sum of the squared differences between the two vectors.

What is pairwise distance in Python?

Let's say you want to compute the pairwise distance between two sets of points, a and b , in Python. The technique works for an arbitrary number of points, but for simplicity make them 2D. Set a has m points giving it a shape of (m, 2) and b has n points giving it a shape of (n, 2) .


1 Answers

The aptly named dist() will do this:

x <- matrix(rnorm(100), nrow=5)
dist(x)

         1        2        3        4
2 7.734978                           
3 7.823720 5.376545                  
4 8.665365 5.429437 5.971924         
5 7.105536 5.922752 5.134960 6.677726

See ?dist for more details

like image 166
Andrie Avatar answered Sep 28 '22 11:09

Andrie