How to find the sum product of two matrix by row in R?

R ProgrammingServer Side ProgrammingProgramming

To find the sum product of two matrix by row in R, we can use rowSums function by passing the multiplication of the matrices.

For example, if we have two matrices say Matrix1 and Matrix2 then, the sum product of these two matrices by row can be found by using the following command −

rowSums(Matrix1*Matrix2)

Example 1

Following snippet creates a matrix −

M1<-matrix(rpois(40,5),ncol=2)
M1

Output

The following matrix is created −

    [,1] [,2]
[1,]  2   5
[2,]  3   4
[3,]  3   7
[4,]  2   3
[5,]  1   5
[6,]  6   4
[7,]  7   2
[8,]  3   1
[9,]  7   6
[10,] 6   4
[11,] 0   3
[12,] 7   3
[13,] 5   4
[14,] 3   6
[15,] 8   2
[16,] 6   5
[17,] 5   6
[18,] 2   6
[19,] 4   5
[20,] 4   5

Following snippet creates a matrix −

M2<-matrix(rpois(40,5),ncol=2)
M2

The following matrix is created −

    [,1] [,2]
[1,]  7   2
[2,]  6   3
[3,]  6   5
[4,]  3   5
[5,]  5   8
[6,]  8   5
[7,]  8  10
[8,]  6   4
[9,]  6   5
[10,] 5   8
[11,] 6   5
[12,] 6   3
[13,] 6   4
[14,] 1   2
[15,] 6   2
[16,] 6   7
[17,] 6   2
[18,] 5   6
[19,] 2   4
[20,] 2   9

To find the sum product of M1 and M2 by row, add the following code to the above snippet −

rowSums(M1*M2)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 24 30 53 21 45 68 76 22 72 62 15 51 46 15 52 71 42 46 28 53

Example 2

Following snippet creates a matrix −

M3<-matrix(rnorm(40),ncol=2)
M3

Output

The following matrix is created −

         [,1]        [,2]
[1,]   0.0851646  -0.17541690
[2,]   0.7304252   0.50273352
[3,]   1.7551681   0.37278626
[4,]  -1.1349055  -1.37850982
[5,]  -1.6318320   0.24106744
[6,]   0.1387407  -1.31176816
[7,]   1.4420244  -1.20419835
[8,]   1.3516549  -0.34028503
[9,]  -0.6722759   0.01012249
[10,] -0.7678689   0.12003056
[11,]  0.4172193  -0.08060120
[12,]  0.1252192   0.28200751
[13,]  1.2772928   1.27433778
[14,] -0.3651585  -0.35230842
[15,] -0.3420042  -0.73562236
[16,]  0.9084724  -0.94662209
[17,] -1.3998868  -1.48221330
[18,]  0.4256211  -0.96901286
[19,]  1.9770241   2.56579038
[20,]  0.2241717  -2.28528391

Following snippet creates a matrix −

M4<-matrix(rnorm(40),ncol=2)
M4

The following matrix is created −

        [,1]          [,2]
[1,]   -0.5194194   -1.39475619
[2,]    0.3652348    0.78122372
[3,]   -0.5823611   -1.05335992
[4,]   -0.8396288   -1.14105115
[5,]    0.4152794   -0.78523122
[6,]   -0.3022416   -0.58049698
[7,]    1.1817702   -0.64439136
[8,]   -0.5036628   -1.39025212
[9,]   -1.5213120    0.37175631
[10,]   1.6345045    0.06372099
[11,]  -0.2278752    0.86347796
[12,]  -0.4476278    0.78871673
[13,]   1.0526197    1.58419381
[14,]   2.2448067    1.98278029
[15,]  -0.7280597    2.02220530
[16,]  -1.2911342   -0.52450314
[17,]   0.3384853   -0.41601927
[18,]  -0.6532896   -0.13330052
[19,]   0.3236825    0.60139393
[20,]   0.2120249    0.41720594

To find the sum product of M3 and M4 by row, add the following code to the above snippet −

rowSums(M3*M4)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.2004277 0.6595241 -1.4148198 2.5258496 -0.8669600 0.7195442
[7] 2.4801165 -0.2076962 1.0265046 -1.2474367 -0.1646713 0.1663724
[13] 3.3633015 -1.5182604 -1.2385799 -0.6764535 0.1427882 -0.1488839
[19] 2.1829789 -0.9059040
raja
Updated on 02-Nov-2021 07:39:45

Advertisements