【问题标题】:Matrix multiplication over three lists of matrices using apply() in R?在R中使用apply()对三个矩阵列表进行矩阵乘法?
【发布时间】:2016-09-28 17:05:44
【问题描述】:

我正在尝试使用 apply() 或类似方法对三个矩阵列表进行矩阵乘法。

以下是示例数据:

mat1 <- matrix(c(1:16), 4, 4, byrow = TRUE)
mat2 <- matrix(c(1:16), 4, 4, byrow = TRUE)
mat3 <- matrix(c(1:16), 4, 4, byrow = TRUE)

l1 <- list(mat1, mat2, mat3)
l2 <- list(mat1, mat2, mat3)
l3 <- list(mat1, mat2, mat3)

对于所有三个列表中的每个对应 [[n]],我想执行以下操作并将它们转储到一个新列表中:

l1[[1]] %*% l2[[1]] %*% l3[[1]]
l1[[2]] %*% l2[[2]] %*% l3[[2]]
l1[[3]] %*% l2[[3]] %*% l3[[3]]

我尝试了以下方法:

mat <- lapply(c(mat1, mat2, mat3), function(x) x[1] %*% x[2] %*% x[3])

但我没有得到我需要的东西。请帮忙?

【问题讨论】:

  • 在纠正李哲元提到的内容后,您可以使用Map(function(x,y,z) x %*% y %*% z, l1, l2, l3) 执行此操作

标签: r matrix


【解决方案1】:

我建议使用以下方法:

mat1 <- matrix(c(1:16), 4, 4, byrow = TRUE)
mat2 <- matrix(c(1:16), 4, 4, byrow = TRUE)
mat3 <- matrix(c(1:16), 4, 4, byrow = TRUE)

l1 <- list(mat1, mat2, mat3)
l2 <- list(mat1, mat2, mat3)
l3 <- list(mat1, mat2, mat3)

f <- function (...) Reduce("%*%", list(...))
mapply(f, l1, l2, l3, SIMPLIFY = FALSE) 

#[[1]]
#      [,1]  [,2]  [,3]  [,4]
#[1,]  3140  3560  3980  4400
#[2,]  7268  8232  9196 10160
#[3,] 11396 12904 14412 15920
#[4,] 15524 17576 19628 21680
#
#[[2]]
#      [,1]  [,2]  [,3]  [,4]
#[1,]  3140  3560  3980  4400
#[2,]  7268  8232  9196 10160
#[3,] 11396 12904 14412 15920
#[4,] 15524 17576 19628 21680
#
#[[3]]
#      [,1]  [,2]  [,3]  [,4]
#[1,]  3140  3560  3980  4400
#[2,]  7268  8232  9196 10160
#[3,] 11396 12904 14412 15920
#[4,] 15524 17576 19628 21680

好处是,您向mapply 提供多少列表并不重要。例如,

mapply(f, l1, l2, l3, l1, l3, l3, SIMPLIFY = FALSE)

也有效。这就是Reduce...的魔力。

【讨论】:

    【解决方案2】:

    看来您想要逐元素乘法。这是通过*-操作符完成的。如果这些在列表中(避免创建一个大向量并丢失所有维度的 c() 操作),那么您可以使用Reduce

    > l1 <- list( mat1, mat2, mat3)
    > 
    > Reduce("*", l1)
         [,1] [,2] [,3] [,4]
    [1,]    1    8   27   64
    [2,]  125  216  343  512
    [3,]  729 1000 1331 1728
    [4,] 2197 2744 3375 4096
    

    如果您希望将 iot 作为向量,则可以使用 c:

     c( Reduce("*", l1) )
     [1]    1  125  729 2197    8  216 1000 2744   27  343 1331 3375   64  512 1728
    [16] 4096
    

    如果按照建议你想要矩阵乘法,那么也许这个(我的矩阵列表):

     c( Reduce("%*%", l1) )
    
     [1]  3140  7268 11396 15524  3560  8232 12904 17576  3980  9196 14412 19628  4400
     [14] 10160 15920 21680
    

    【讨论】:

    • 我将访问“[[n]]”元素的请求解释为元素方面的。我可能错了。
    【解决方案3】:

    这是你想要的矩阵乘法

    mat1 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))
    mat2 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))
    mat3 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))
    
    l1 <- list(mat1, mat2, mat3)
    l2 <- list(mat1, mat2, mat3)
    l3 <- list(mat1, mat2, mat3)
    
    matrix(unlist(l1[[1]]),4,4) %*% matrix(unlist(l2[[1]]),4,4) %*% matrix(unlist(l3[[1]]),4,4)
    matrix(unlist(l1[[2]]),4,4) %*% matrix(unlist(l2[[2]]),4,4) %*% matrix(unlist(l3[[2]]),4,4)
    matrix(unlist(l1[[3]]),4,4) %*% matrix(unlist(l2[[3]]),4,4) %*% matrix(unlist(l3[[3]]),4,4)
    

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多