How to add or multiply each element of a matrix to the corresponding element of another matrix in R, if these matrices are stored as a list?

R ProgrammingServer Side ProgrammingProgramming

Basic mathematical operations such as addition, subtraction, multiplication, and division are common for matrices and we often do that but if the matrices are stored as a list in R then these basic calculations are done differently as they are not direct objects. To add or multiply the matrices in a list, we can use Reduce function with the plus (+) or multiply (*) sign and the list name.

Example

 Live Demo

Consider the below list of matrices −

Matrices_List<-list(matrix(1:100,nrow=10),matrix(1:100,nrow=10),matrix(1:100,nrow=10))
Matrices_List

Output

[[1]]
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
[[2]]
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
[[3]]
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
Reduce('+',Matrices_List)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 33 63 93 123 153 183 213 243 273
[2,] 6 36 66 96 126 156 186 216 246 276
[3,] 9 39 69 99 129 159 189 219 249 279
[4,] 12 42 72 102 132 162 192 222 252 282
[5,] 15 45 75 105 135 165 195 225 255 285
[6,] 18 48 78 108 138 168 198 228 258 288
[7,] 21 51 81 111 141 171 201 231 261 291
[8,] 24 54 84 114 144 174 204 234 264 294
[9,] 27 57 87 117 147 177 207 237 267 297
[10,] 30 60 90 120 150 180 210 240 270 300
Reduce('*',Matrices_List)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1331 9261 29791 68921 132651 226981 357911 531441 753571
[2,] 8 1728 10648 32768 74088 140608 238328 373248 551368 778688
[3,] 27 2197 12167 35937 79507 148877 250047 389017 571787 804357
[4,] 64 2744 13824 39304 85184 157464 262144 405224 592704 830584
[5,] 125 3375 15625 42875 91125 166375 274625 421875 614125 857375
[6,] 216 4096 17576 46656 97336 175616 287496 438976 636056 884736
[7,] 343 4913 19683 50653 103823 185193 300763 456533 658503 912673
[8,] 512 5832 21952 54872 110592 195112 314432 474552 681472 941192
[9,] 729 6859 24389 59319 117649 205379 328509 493039 704969 970299
[10,] 1000 8000 27000 64000 125000 216000 343000 512000 729000 1000000
raja
Published on 21-Aug-2020 06:18:34
Advertisements