# How to find the dot product of two matrices in R?

To find the dot product of two matrices in R, we can use dot function of geometry package.

For Example, if we have two matrices say matrix1 and matrix2 then we can use the dot product of these two matrices by using the below given command −

dot(matrix1,matrix2)

## Example 1

Following snippet creates a sample matrix −

M1<-matrix(rpois(25,5),ncol=5)
M1

The following matrix is created −

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

Following snippet creates a sample matrix −

M2<-matrix(rpois(25,5),ncol=5)
M2

The following matrix is created −

   [,1] [,2] [,3] [,4] [,5]
[1,] 7    4    6    3    7
[2,] 6    1    5    2    5
[3,] 3    7    3    6    3
[4,] 6    5    5    4    3
[5,] 3    7    3    5    6

To load geometry package and find the dot product of matrices M1 and M2 on the above created matrices, add the following code to the above snippet −

M1<-matrix(rpois(25,5),ncol=5)
M2<-matrix(rpois(25,5),ncol=5)
library(geometry)
dot(M1,M2)

## Output

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

[1] 133 132 113 89 174

## Example 2

Following snippet creates a sample matrix −

M3<-matrix(rpois(80,5),ncol=4)
M3

The following matrix is created −

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

Following snippet creates a sample matrix −

M4<-matrix(rpois(80,2),ncol=4)
M4

The following matrix is created −

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

To find the dot product of matrices M3 and M4 on the above created matrices, add the following code to the above snippet −

M3<-matrix(rpois(80,5),ncol=4)
M4<-matrix(rpois(80,2),ncol=4)
dot(M3,M4)

## Output

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

[1] 141 170 159 211


## Example 3

Following snippet creates a sample matrix −

M5<-matrix(round(rnorm(60),1),ncol=3)
M5


The following matrix is created −

      [,1] [,2] [,3]
[1,]  0.0 -0.4 -0.6
[2,]  0.2  1.5 -0.2
[3,]  0.3  0.2  0.7
[4,] -0.8  0.1 -1.2
[5,]  0.9  1.1  1.2
[6,] -0.1 -0.2 -0.9
[7,]  1.1 -1.4 -0.2
[8,]  0.5 -0.9 -0.3
[9,]  0.1 -2.6  0.2
[10,] -0.4  0.9  1.2
[11,]  0.6  0.4  0.7
[12,]  3.1 -3.3  0.0
[13,]  0.2 -0.4  2.5
[14,]  1.7  1.0 -0.9
[15,]  0.0  0.8 -0.2
[16,] -0.1  0.9 -0.2
[17,]  0.6  0.5 -2.3
[18,]  0.1  0.4 -1.7
[19,] -0.1  0.4 -1.3
[20,]  0.9  0.6  0.4

Following snippet creates a sample matrix −

M6<-matrix(round(rnorm(60),1),ncol=3)
M6

The following matrix is created −

      [,1] [,2] [,3]
[1,]  1.5 -1.1 -2.4
[2,]  1.9  0.0 -0.1
[3,] -1.5 -0.5 -1.0
[4,]  1.2  0.9 -0.1
[5,]  0.3  0.5  0.3
[6,]  1.1 -0.1  0.5
[7,]  0.1  1.2  1.6
[8,]  0.1  1.0 -0.8
[9,] -0.4 -1.6 -0.8
[10,] -0.2 -0.6  0.4
[11,]  2.3 -3.6  0.7
[12,] -2.1  1.5  0.2
[13,] -0.4 -1.9 -1.0
[14,] -0.8 -0.5  0.1
[15,]  0.4  0.4  0.9
[16,] -0.3  1.2  0.7
[17,]  0.4 -1.8 -0.1
[18,] -0.8  0.2 -0.2
[19,] -0.1  2.1 -2.8
[20,] -0.7 -0.6  1.3

To find the dot product of matrices M5 and M6 on the above created matrices, add the following code to the above snippet −

M5<-matrix(round(rnorm(60),1),ncol=3)
M6<-matrix(round(rnorm(60),1),ncol=3)
dot(M5,M6)

## Output

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

[1] -7.67 -3.03 3.34