点积

时间:2018-07-19 15:46:43

标签: r purrr

如何计算purrr中的点积?作为一个代表,这是一个简单的示例。

数据生成

 #fake data 
 X <- as_tibble(list(a = rnorm(10,0,1), 
        b = rnorm(10,10,1), 
        c = rnorm(10,100,1)))
 z <- c(1,0,1) 

 #make tibble matrix
 X_matrix <- X %>% as.matrix()
 X_matrix
                a         b         c
 [1,]  0.01182775  9.032966 100.95322
 [2,]  0.85718250 10.015310 102.30181
 [3,] -0.06742915 10.535482 100.21764
 [4,] -0.18236798  9.052234  99.37345
 [5,] -0.32151084 10.329401  98.81186
 [6,]  2.94303948  9.994800  99.93874
 [7,]  0.03299169  9.079023  99.73501
 [8,]  0.06518171  8.841637  99.91130
 [9,] -0.71944580 10.281631 100.32533
[10,]  1.49983359 10.776108  99.35903

计算点积

点积为sum(a*z[1] + b*z[2] + c*z[3])

 X_matrix %*% z
           [,1]
 [1,] 100.96505
 [2,] 103.15900
 [3,] 100.15021
 [4,]  99.19108
 [5,]  98.49035
 [6,] 102.88178
 [7,]  99.76800
 [8,]  99.97648
 [9,]  99.60588
[10,] 100.85886

理想情况下,我想将点积作为列添加到X

0 个答案:

没有答案