In R: How to write matrix elements without a loop

In R: How to perform a log operation for each matrix element without using a loop?

I have a matrix m, and I want each element to be replaced by its log. log (m) does not work.

params = array(list(),c(2, 2))

then I manually set all the elements.

params

[, 12]

[1,] 3 3

[2,] 3 3

log(params)

Error in log (params): non-numeric argument for math function

+3
source share
3 answers

log(M) works for all of us on the right matrices:

R> M <- matrix(1:4,2)
R> M
     [,1] [,2]
[1,]    1    3
[2,]    2    4
R> log(M)
        [,1]   [,2]
[1,] 0.00000 1.0986
[2,] 0.69315 1.3863
R> 

Can you show us yours M?

+4
source

You were not able to create a matrix - this way you have a list matrix and you need a matrix of numbers.
Try the following:

params<-array(3,c(2,2))
log(params)
+1
source

, 0. , , , 0.

?

, log (0) .

0

Source: https://habr.com/ru/post/1779795/


All Articles