QwayneQ
QwayneQ

Reputation: 43

Calculate the distances between pairs of points in r

Lets say I have generated 10 random points

x <- runif(10, min = -10, max = 10)
y <- runif(10, min = -10, max = 10)

and I want to calculate the distances between each pair of points. so I use

d <- dist(cbind(x,y)) 

and I got a nice 9*9 matrix.

However, if I use

d1 <- dist(rbind(x,y))

I only got 1 number as the result.

Can anyone explain this for me?

Upvotes: 4

Views: 5562

Answers (1)

fishtank
fishtank

Reputation: 3728

rbind(x,y) has 2 rows, 10 columns and is interpreted as 2 points in 10-dimensional space. dist(rbind(x,y)) is calculating the Euclidean distance between these 2 points.

Upvotes: 5

Related Questions