How to multiply two matrices by elements in R?


To multiply two matrices by elements in R, we would need to use one of the matrices as vector. For example, if we have two matrices defined by names M1 and M2 then the multiplication of these matrices by elements can be done by using M1*as.vector(M2). The main thing we need to remember while doing this kind of multiplication is that the number of rows in both the matrices are equal.

Example

 Live Demo

M1<-matrix(1:25,ncol=5)
M1

Output

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

Example

 Live Demo

M2<-matrix(1:25,ncol=5)
M2

Output

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

Example

M1*as.vector(M2)

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1    36  121 256  441
[2,] 4    49  144 289  484
[3,] 9    64  169 324  529
[4,] 16   81  196 361  576
[5,] 25  100  225 400  625

Example

 Live Demo

M3<-matrix(rnorm(36),nrow=6)
M3

Output

         [,1]        [,2]       [,3]       [,4]       [,5]    [,6]
[1,] -0.66269194 -1.7457905 0.4271430 1.81482482 -1.52828812 0.1507425
[2,] 0.09297130 -0.5364919 0.8380242 -0.02126481 -0.03926208 0.2150862
[3,] 0.84724054 1.2285630 -0.8867815 1.27482991 0.76055452 0.3949002
[4,] -1.65257106 -0.7111136 2.0442647 -0.69539458 -1.89791811 1.8776415
[5,] -1.65376676 -1.7332403 0.5251878 1.20379635 0.21809336 -1.4143876
[6,] 0.09506334 -0.1884270 1.8049270 0.64850025 -0.36684996 0.7390629

Example

 Live Demo

M4<-matrix(rnorm(36),nrow=6)
M4

Output

         [,1]       [,2]       [,3]       [,4]       [,5]    [,6]
[1,] 0.7435446 -1.6258520 -0.3318020 -1.3084350 0.2875227 -0.99886827
[2,] -1.6527269 -0.5240276 -0.6695268 0.2736843 0.9960383 -1.03338278
[3,] -2.0807263 -1.1482815 -0.6361266 -2.6593496 0.5059342 -0.07586589
[4,] 0.6829553 0.6814575 -0.1174571 -0.4193767 -0.6077792 -0.04488023
[5,] -0.4490323 -1.0317825 -1.7346983 0.8237790 0.6099129 0.42651656
[6,] 0.8599890 -0.1197610 1.1092640 -0.6074690 0.6888512 -0.58980418

Example

M3*as.vector(M4)

Output

         [,1]          [,2]       [,3]       [,4]       [,5]       [,6]
[1,] -0.49274103 2.83839710 -0.1417269 -2.374580244 -0.43941754 -0.15057195
[2,] -0.15365618 0.28113657 -0.5610796 -0.005819844 -0.03910654 -0.22226640
[3,] -1.76287571 -1.41073618 0.5641054 -3.390218430 0.38479058 -0.02995946
[4,] -1.12863221 -0.48459370 -0.2401134 0.291632294 1.15351521 -0.08426899
[5,] 0.74259470 1.78832703 -0.9110424 0.991662178 0.13301795 -0.60325973
[6,] 0.08175343 0.02256622 2.0021405 -0.393943794 -0.25270505 -0.43590241

Example

 Live Demo

M5<-matrix(rpois(64,5),nrow=8)
M5

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3    3    2    3    4    7    10   6
[2,] 6    6    7    4    2    7    7    0
[3,] 6    3    9    8    12   8    7    5
[4,] 4    6    4    7    3    2    5    4
[5,] 4    5    6    6    4    5    6    4
[6,] 7    7    8    4    2    5    3    6
[7,] 2    0    4    6    3    5    5    7
[8,] 7    4    3    3    10   8    8    4

Example

 Live Demo

M6<-matrix(rpois(64,5),nrow=8)
M6

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 5    9    5    6    9    5    7    9
[2,] 6    5    4    13   7    4    6    4
[3,] 8    4    5    5    8    7    1    6
[4,] 1    4    1    3    5    3    5    2
[5,] 5    7    5    2    5    4    10   3
[6,] 2    6    6    3    4    8    2    6
[7,] 10   6    7    7    6    6    8    4
[8,] 9    1    4    11   5    7    6    2

Example

M5*as.vector(M6)

Output

    [,1] [,2]  [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 15    27    10    18    36 35   70 54
[2,] 36    30    28    52    14 28   42 0
[3,] 48    12    45    40    96 56    7 30
[4,] 4     24     4    21    15 6    25 8
[5,] 20    35    30    12    20 20   60 12
[6,] 14    42    48    12    8 40    6 36
[7,] 20    0     28    42    18 30   40 28
[8,] 63    4     12    33    50 56   48 8

Example

 Live Demo

M8−-matrix(sample(0:9,36,replace=TRUE),nrow=6)
M8

Output

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    1    3    3    2    9
[2,] 2    2    1    4    3    6
[3,] 5    3    4    0    0    8
[4,] 4    0    9    1    5    8
[5,] 8    9    4    7    0    5
[6,] 2    3    1    5    8    1

Example

M7*as.vector(M8)

Output

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 4    7    24    9    4    63
[2,] 6    14    3    24   6    24
[3,] 0    24   36    0    0    16
[4,] 12   0    45    3    45   0
[5,] 16   18    4    14   0    0
[6,] 10   21    9    10   32   3

Example

 Live Demo

M9<-matrix(sample(501:999,16),ncol=4)
M9

Output

   [,1] [,2] [,3] [,4]
[1,] 591 926 909 660
[2,] 872 976 625 868
[3,] 801 929 690 697
[4,] 992 599 662 879

Example

 Live Demo

M10<-matrix(sample(501:999,16),ncol=4)
M10

Output

   [,1] [,2] [,3] [,4]
[1,] 731 772 750 854
[2,] 798 858 641 635
[3,] 613 579 560 790
[4,] 589 760 504 876

Example

M9*as.vector(M10)

Output

      [,1]    [,2] [,3]    [,4]
[1,] 432021 714872 681750 563640
[2,] 695856 837408 400625 551180
[3,] 491013 537891 386400 550630
[4,] 584288 455240 333648 770004

Example

 Live Demo

M11<-matrix(sample(501:999,16),ncol=4)
M11

Output

   [,1] [,2] [,3] [,4]
[1,] 690 550 737 749
[2,] 771 519 652 745
[3,] 522 841 733 825
[4,] 842 752 800 934

Example

 Live Demo

M12<-matrix(sample(501:999,8),ncol=2)
M12

Output

   [,1] [,2]
[1,] 969 784
[2,] 648 833
[3,] 914 808
[4,] 947 585

Example

M11*as.vector(M12)

Output

      [,1]    [,2] [,3]    [,4]
[1,] 668610 431200 714153 587216
[2,] 499608 432327 422496 620585
[3,] 477108 679528 669962 666600
[4,] 797374 439920 757600 546390

Updated on: 08-Dec-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements