小编sda*_*ata的帖子

R - 比较两个数据集时如何避免循环?

版本以简化问题

我有两个矩阵:

  • mat1:nrow = 100 000; NcoI位= 5
  • mat2:nrow = 500 000; NcoI位= 5

预期成绩

计算每行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))

loops r vectorization

5
推荐指数
1
解决办法
238
查看次数

标签 统计

loops ×1

r ×1

vectorization ×1