简体   繁体   中英

R - Manhattan / Euclidean distance calculations into a matrix

I would like to ask help on distance measures for continuous variables

There is an example:

x1 = (0,0)
x2 = (1,0)
x3 = (5,5)

The example is to find the distance matrix for L1-norm and L2-norm(Euclidean). I don't know how to compute in R to get the following answer:

在此处输入图片说明

I have tried to do it like this but it didn't work as expected.

y2 <- c(0,0)
y3 <- c(1,0)
y4 <- c(5,5)
y5 <- rbind(y2,y3,y4)
dist(y5)
y2 <- c(0,0)
y3 <- c(1,0)
y4 <- c(5,5)

mat <- rbind(y2, y3, y4)

d1 <- dist(mat, upper=TRUE, diag=TRUE, method="manhattan")
d1
#    y2 y3 y4
# y2  0  1 10
# y3  1  0  9
# y4 10  9  0

d2 <- dist(mat, upper=TRUE, diag=TRUE)^2
d2
#    y2 y3 y4
# y2  0  1 50
# y3  1  0 41
# y4 50 41  0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM