矩阵乘以R中的向量

时间:2013-05-10 05:53:35

标签: r

我有一个矩阵m和一个向量v。我想将矩阵m乘以vetcor v并获得与m具有相同维度的矩阵,意味着将m的第一个元素乘以v并且....我怎么能在R?中做到这一点?

    m = matrix(c(1, 2, 3, 4, 5), ncol=1)
    v = c(1, 2, 3, 4, 5)
    > z
      [,1]
 [1,]    1
 [2,]    4
 [3,]    9
 [4,]   16
 [5,]   25

1 个答案:

答案 0 :(得分:0)

Cross products可以使用%*%运算符获取:

> m = matrix(c(1, 2, 3, 4, 5), ncol=1)
>     v = c(1, 2, 3, 4, 5)

> m %*% v
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15
[4,]    4    8   12   16   20
[5,]    5   10   15   20   25
> m * v
     [,1]
[1,]    1
[2,]    4
[3,]    9
[4,]   16
[5,]   25