Replace array value (R)

1

I need to replace the value that appears on the main diagonal of my matrix with another, how to proceed?

    
asked by anonymous 30.06.2017 / 17:01

1 answer

2
M <- matrix(rpois(25, lambda = 4), ncol = 5)
M
 [,1] [,2] [,3] [,4] [,5]
[1,]    4    3    4    3    6
[2,]    3    2    6    1    6
[3,]    3    4    6    4    1
[4,]    9    3    3    4    4
[5,]    3    5    6    3    8

valor <- diag(M)
valor
[1] 4 2 6 4 8

M <- (M**M)*10
M
           [,1]  [,2]   [,3] [,4]      [,5]
[1,]       2560   270   2560  270    466560
[2,]        270    40 466560   10    466560
[3,]        270  2560 466560 2560        10
[4,] 3874204890   270    270 2560      2560
[5,]        270 31250 466560  270 167772160

diag(M) <- valor
M
           [,1]  [,2]   [,3] [,4]   [,5]
[1,]          4   270   2560  270 466560
[2,]        270     2 466560   10 466560
[3,]        270  2560      6 2560     10
[4,] 3874204890   270    270    4   2560
[5,]        270 31250 466560  270      8
    
30.06.2017 / 19:11