计算列表中所有数据框之间的Hausdorff距离

时间:2019-05-10 11:59:02

标签: r for-loop matrix distance

我有一个数据帧列表,每个帧有4列,行数不同。我想为列表中的所有数据帧计算Hausdorff距离,并填充结果矩阵。

library(pracma)
建立清单
    data_frame1<-data.frame("ID"=1:10, "Sum1"= c(4, -2, 16, 20, 13, 4,2,0, 0, -12),  "Sum2"=c(-3,-6, 0, 10, 6, 2, 4, -8, 0, 0))
    data_frame2<-data.frame("ID"=1:6, "Sum1"= c(13, 4,2,0, 0, -12),  "Sum2"=c(10, 6, 2, 4, -8, 0))
    data_frame3<-data.frame("ID"=1:5, "Sum1"= c(9,3,5,-6,-1),  "Sum2"=c(-2,-4,5, 20,38))
    data_frame4<-data.frame("ID"=1:12, "Sum1"= c(3,4, 9,3,5,-5, -6,-1,0,16,11,9),  "Sum2"=c(-2,8,8,17, 14,-4,5, 21,30, 10, -15,3))


my_list<- list(data_frame1, data_frame2, data_frame3, data_frame4)
创建4x4矩阵以将其填充到距离结果中
hausdist<-matrix(, 4,4)

i<-1:4
注意我只想计算第2列和第3列的距离
P<-as.matrix(my_list[[i]][c(2,3)])
Q<-as.matrix(my_list[[i+1]][c(2,3)])
### Loop for calculation of hausdorff distance.
for (i in (i+1):3) { 
  hausdist[[i]]<- print(hausdorff_dist(P,Q))
}
print(hausdist)
 [,1]     [,2] [,3] [,4]
[1,]       NA 13.15295   NA   NA
[2,]       NA       NA   NA   NA
[3,] 13.15295       NA   NA   NA
[4,] 13.15295       NA   NA   NA

#### Correct values:
hausdorff_dist(as.matrix(my_list[[3]][c(2,3)]),as.matrix(my_list[[4]][c(2,3)]))
[1] 13.15295
> hausdorff_dist(as.matrix(my_list[[1]][c(2,3)]),as.matrix(my_list[[2]][c(2,3)]))
[1] 10.44031

分别计算距离时,结果是正确的,但对于循环,我对所有数据帧对都具有相同的值。

2 个答案:

答案 0 :(得分:2)

尝试两次sapply循环。

hd <- function(X, Y){
  P <- as.matrix(X[2:3])
  Q <- as.matrix(Y[2:3])
  hausdorff_dist(P, Q)
}

sapply(my_list, function(X){
  sapply(my_list, function(Y) hd(X, Y))
})
#         [,1]     [,2]     [,3]     [,4]
#[1,]  0.00000 10.44031 34.13210 26.07681
#[2,] 10.44031  0.00000 31.30495 23.85372
#[3,] 34.13210 31.30495  0.00000 13.15295
#[4,] 26.07681 23.85372 13.15295  0.00000

尽管我发现以前的解决方案更直观,但这是使用outerMap的单一代码。

outer(my_list, my_list, FUN = function(x, y) Map(hd, x, y))
#     [,1]     [,2]     [,3]     [,4]    
#[1,] 0        10.44031 34.1321  26.07681
#[2,] 10.44031 0        31.30495 23.85372
#[3,] 34.1321  31.30495 0        13.15295
#[4,] 26.07681 23.85372 13.15295 0       

答案 1 :(得分:0)

您可以在数据帧的所有排列中使用apply

# extract only the relevant columns
l1 <- lapply(my_list, function(x) as.matrix(x)[,2:3])
# create all permutations of the data sets
combs <- t(expand.grid(l1,l1))
# calculate distances for each combination and rearrange
d <- apply(combs, 2, function(x) hausdorff_dist(x[1][[1]], x[2][[1]]))
matrix(d, nrow = length(my_list), ncol = length(my_list))

#      [,1]     [,2]     [,3]     [,4]
#[1,]  0.00000 10.44031 34.13210 26.07681
#[2,] 10.44031  0.00000 31.30495 23.85372
#[3,] 34.13210 31.30495  0.00000 13.15295
#[4,] 26.07681 23.85372 13.15295  0.00000