1

how can I take exp() of each element in a matrix? I have mymatrix = matrix(c(2, 4, 3, 1, 5, 7), 3,2) and tried using res<-expm(mymatrix) but it requires mymatrix to be square. Is there another way to calculate each element so res is matrix(c(exp(2), exp(4), exp(3), exp(1), exp(5), exp(7), 3,2) ?

5
  • 9
    exp(your_matrix) Commented Apr 3, 2017 at 18:38
  • 1
    Btw, there is no expm in R. You should be clear about what packages you're using. Commented Apr 3, 2017 at 18:44
  • Thank it worked. Not sure why I didn't think of that :S Commented Apr 3, 2017 at 18:44
  • I tried the command on the dummie matrix in this example and it works perfect! However when I try it on my bigger matrix I get following error message Error in expm(Matrix(x)) : Matrix exponential requires square, non-null matrix Yes my matrix is not square, but neither is my dummie matrix. I have looked at each cell of the matrix and there are no cells=0. I'm confused Commented Apr 3, 2017 at 19:00
  • You're confusing a matrix exponential with taking the exp() of each element. These are different operations. A matrix exponential is defined by a power series, and powers are only defined for square matrices. Commented Apr 3, 2017 at 19:57

1 Answer 1

2
res <- mymatrix res [] <- exp(res) > res [,1] [,2] [1,] 7.389056 2.718282 [2,] 54.598150 148.413159 [3,] 20.085537 1096.633158 

Here you go.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.