Automatic multiplication between vector and matrix

前端 未结 4 983
挽巷
挽巷 2021-01-19 21:52

I have this R code:

> coef
[1] 1.5 2.4 3.9 4.4
> y
     [,1] [,2] [,3] [,4]
[1,]    1    2   12   45
[2,]    5    6    7    8
[3,]    9   10    2   12
         


        
4条回答
  •  旧时难觅i
    2021-01-19 22:23

     apply(y, 1, "*", coef)
    #  -- result --    
      [,1] [,2] [,3]  [,4]  [,5]
    [1,]   1.5  7.5 13.5  19.5  25.5
    [2,]   4.8 14.4 24.0  33.6  43.2
    [3,]  46.8 27.3  7.8  58.5 152.1
    [4,] 198.0 35.2 52.8 198.0  30.8
    

提交回复
热议问题