R 跨矩阵匹配列
假设我有两个或更多的矩阵。矩阵中的行数和列数相同。但是矩阵不一定是平方的R 跨矩阵匹配列,r,combinations,permutation,distance,similarity,R,Combinations,Permutation,Distance,Similarity,假设我有两个或更多的矩阵。矩阵中的行数和列数相同。但是矩阵不一定是平方的 Matrix1 a b c 1 0.911 0.067 0.023 2 0.891 0.089 0.019 3 0.044 0.931 0.025 4 0.919 0.058 0.023 Matrix2 a b c 1 0.024 0.070 0.906 2
Matrix1
a b c
1 0.911 0.067 0.023
2 0.891 0.089 0.019
3 0.044 0.931 0.025
4 0.919 0.058 0.023
Matrix2
a b c
1 0.024 0.070 0.906
2 0.020 0.090 0.891
3 0.025 0.930 0.045
4 0.024 0.058 0.918
行的总和始终为1。列可能会在矩阵之间移动位置。所以列名没有什么意义。上述示例中,材料表1中的“a”列是材料表2中的“c”列。该值将不相同,但相似
我可以使用什么样的方法/算法来对齐许多这样的矩阵中的列
预期结果如下所示
Matrix1
a b c
1 0.911 0.067 0.023
2 0.891 0.089 0.019
3 0.044 0.931 0.025
4 0.919 0.058 0.023
Matrix2
c b b
1 0.906 0.070 0.024
2 0.891 0.090 0.020
3 0.045 0.930 0.025
4 0.918 0.058 0.024
确保各列对齐。”mat1中的“a”对应于mat2中的“c”,依此类推。在这个可能的结果中,mat1是参考,mat2与之对齐
如果有人想尝试一些东西,我会使用R
mat1 <-
matrix(c(0.911,0.891,0.044,0.919,0.067,0.089,0.931,0.058,0.023,0.019,0.025,0.023),nrow=4)
mat2 <-
matrix(c(0.024,0.020,0.025,0.024,0.070,0.090,0.930,0.058,0.906,0.891,0.045,0.918),nrow=4)
假设每一列总是有一个很好的匹配,这应该是可行的
Matrix2[, sapply(1:ncol(Matrix1),
function(i) which.min(colSums(abs(Matrix2 - Matrix1[,i]))))]
c b a
1 0.906 0.070 0.024
2 0.891 0.090 0.020
3 0.045 0.930 0.025
4 0.918 0.058 0.024
你可以这样做。该函数返回mat的列索引,其顺序与m.base列的欧几里德距离最匹配
请显示所需结果,因为在许多这样的矩阵中对齐列并不清楚。基本上,您希望识别矩阵2中的列,并将它们命名为矩阵1中最相似的列?我已编辑以添加所需结果。可能简单到mat2[,ordermat1[1,]-mat2[1,]]或mat2[,max.col-outermat1[1,],mat2[1,],functioni,j absi-j]
col.order <- function(m.base, mat){
no.cols <- ncol(mat)
col.ord <- rep(NA, no.cols)
for(i in 1:no.cols){
vec <- m.base[, i]
col.dists <- apply(mat, 2, function(x) sum((x-vec)^2))
best.dist <- min(col.dists[is.na(col.ord)])
best.col <- match(best.dist, col.dists)
col.ord[best.col] <- i
}
return(col.ord)
}
mat2[, col.order(mat1,mat2)]
[,1] [,2] [,3]
[1,] 0.906 0.070 0.024
[2,] 0.891 0.090 0.020
[3,] 0.045 0.930 0.025
[4,] 0.918 0.058 0.024