简体   繁体   中英

How do I apply functions to matrices?

Are there any packages/functions in R that allow users to apply functions to matrices?

For example the Taylor expansion of the matrix M when the exponential function is applied is 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)

This is not what I want. Does anyone know if it is possible to apply matrix functions in R?

The answer goes to @Ben Bolkner. You can use expm function of Matrix package.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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