简体   繁体   English

如何将函数应用于矩阵?

[英]How do I apply functions to matrices?

Are there any packages/functions in R that allow users to apply functions to matrices? R 中是否有任何包/函数允许用户将函数应用于矩阵?

For example the Taylor expansion of the matrix M when the exponential function is applied is exp(M)=1+M+M^2+M^3+...例如,当应用指数函数时,矩阵 M 的泰勒展开式为 exp(M)=1+M+M^2+M^3+...

Now let:现在让:

M<-matrix(1:4,nrow=2)

But if I were to input the command exp(M) in R, it would just give me the matrix: matrix(c(exp(1),exp(2),exp(3),exp(4)),nrow=2)但是如果我在 R 中输入命令exp(M) ,它只会给我矩阵: matrix(c(exp(1),exp(2),exp(3),exp(4)),nrow=2)

This is not what I want.这不是我想要的。 Does anyone know if it is possible to apply matrix functions in R?有谁知道是否可以在 R 中应用矩阵函数?

The answer goes to @Ben Bolkner.答案是@Ben Bolkner。 You can use expm function of Matrix package.您可以使用Matrix包的expm函数。

Please see the code below:请看下面的代码:

library(Matrix)
M < -matrix(1:4,nrow=2)
expm(M)

Output:输出:

2 x 2 Matrix of class "dgeMatrix"
         [,1]     [,2]
[1,] 51.96896 112.1048
[2,] 74.73656 164.0738

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM