【问题标题】:Multiply elements of a matrix with vector values将矩阵的元素与向量值相乘
【发布时间】:2018-02-08 03:52:17
【问题描述】:

我有一个矩阵 M,我想创建 3 个附加矩阵,其中每个附加矩阵具有 M 的某些 3x3 列切片乘以向量中的值,然后我将生成的 3 个新矩阵存储在 list 中。

##create the initial matrix
M <- matrix(1:20, nrow = 4)

    [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

## coordinates in the matrix I want to alter 
iy <- c(1, 2, 3)
ix <- c(1, 4, 5)
coords <- as.data.frame(cbind(ix, iy))


## multiplier values
multis <- c(0.1, 2, 100)

我想做的伪代码

mapply (function(multis, cords) {multis * M[coords$iy, coords$ix]})

结果应该是什么样子

  [[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.1    5    9 13.0 17.0
[2,]  2.0    6   10  1.4 18.0
[3,]  3.0    7   11 15.0  1.1
[4,]  4.0    8   12 16.0 20.0

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    5    9   13   17
[2,]    2    6   10   28   18
[3,]    3    7   11   15   38
[4,]    4    8   12   16   20

[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,]  100    5    9   13   17
[2,]    2    6   10 1400   18
[3,]    3    7   11   15 1900
[4,]    4    8   12   16   20

【问题讨论】:

  • 您需要清楚乘以的意思是“逐元素乘以 3x1 行向量”。不是矩阵乘法。

标签: r matrix elementwise-operations


【解决方案1】:

首先,您需要将coords 强制转换为矩阵以进行索引,然后反转列顺序。那么它只是一个简单的lapply() 循环。

coords <- as.matrix(coords)[, 2:1]
lapply(multis, function(x) {
    M[coords] <- M[coords] * x
    M
})

导致

[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.1    5    9 13.0 17.0
[2,]  2.0    6   10  1.4 18.0
[3,]  3.0    7   11 15.0  1.9
[4,]  4.0    8   12 16.0 20.0

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    5    9   13   17
[2,]    2    6   10   28   18
[3,]    3    7   11   15   38
[4,]    4    8   12   16   20

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]  100    5    9   13   17
[2,]    2    6   10 1400   18
[3,]    3    7   11   15 1900
[4,]    4    8   12   16   20

【讨论】:

    【解决方案2】:

    另一种解决方案是使用已定义的函数并为每个multis 使用sapply

    ##create the initial matrix
    M <- matrix(1:20, nrow = 4)
    
    ## coordinates in the matrix I want to alter 
    Y <- c(1, 2, 3)
    X <- c(1, 4, 5)
    coords <- as.data.frame(cbind(X, Y))
    
    ## multiplier values
    multis <- c(0.1, 2, 100)
    
    ## Modifying the specific coordinates.
    modif.one.matrix <- function(one_multis, coords, M) {
        M_out <- M
        for(one_coord in 1:nrow(coords)) {
            M_out[coords$Y[one_coord], coords$X[one_coord]] <- M[coords$Y[one_coord], coords$X[one_coord]] * one_multis
        }
        return(M_out)
    }
    
    ## Modifying one matrix
    modif.one.matrix(multis[1], coords, M)
    #     [,1] [,2] [,3] [,4] [,5]
    #[1,]  0.1    5    9 13.0 17.0
    #[2,]  2.0    6   10  1.4 18.0
    #[3,]  3.0    7   11 15.0  1.9
    #[4,]  4.0    8   12 16.0 20.0
    
    ## Modifying all the matrices
    sapply(multis, modif.one.matrix, coords, M, simplify = FALSE)
    
    #[[1]]
    #     [,1] [,2] [,3] [,4] [,5]
    #[1,]  0.1    5    9 13.0 17.0
    #[2,]  2.0    6   10  1.4 18.0
    #[3,]  3.0    7   11 15.0  1.9
    #[4,]  4.0    8   12 16.0 20.0
    #
    #[[2]]
    #     [,1] [,2] [,3] [,4] [,5]
    #[1,]    2    5    9   13   17
    #[2,]    2    6   10   28   18
    #[3,]    3    7   11   15   38
    #[4,]    4    8   12   16   20
    #
    #[[3]]
    #     [,1] [,2] [,3] [,4] [,5]
    #[1,]  100    5    9   13   17
    #[2,]    2    6   10 1400   18
    #[3,]    3    7   11   15 1900
    #[4,]    4    8   12   16   20
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多