我有两个矩阵:
计算每行mat1与每行之间相似数字的数量mat2:
Intersection <- function(matrix1, matrix2){
Intersection = matrix(nrow=nrow(matrix1), ncol=ncol(matrix2))
for(i in 1:nrow(matrix3)) {
for(j in 1:ncol(matrix3)) {
Intersection[i,j] = length(intersect(matrix1[i,], matrix2[j,])
}
}
return(Intersection) }
Run Code Online (Sandbox Code Playgroud)
如何向量化这个函数以避免循环?
以下是为了试验解决方案而提供的数据示例:
dput(matrix1)结构(c(1L,20L,2L,1L,7L,2L,22L,12L,2L,27L,3L,35L,16L,3L,32L,4L,37L,35L,17L,33L,5L, 38L,46L,27L,49L),. Dim = c(5L,5L))
dput(matrix2)结构(c(1,14,7,1,7,2,22,12,2,27,7,35,16,3,32,14,39,35,17,32,17, 38,46,20,49),. Dim = c(5L,5L))