# How to multiply a matrix columns and rows with the same matrix rows and columns in R?

R ProgrammingServer Side ProgrammingProgramming

To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.

## Example

Live Demo

M<−matrix(1:100,ncol=10)
M

## Output

[,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
M[1,]%*%M[,1]
[,1]
[1,] 3355
M[1,]%*%M[,2]
[,1]
[1,] 7955
M[1,]%*%M[,3]
[,1]
[1,] 12555
M[1,]%*%M[,5]
[,1]
[1,] 21755
M[1,]%*%M[,7]
[,1]
[1,] 30955
M[1,]%*%M[,8]
[,1]
[1,] 35555
M[1,]%*%M[,10]
[,1]
[1,] 44755
M[2,]%*%M[,1]
[,1]
[1,] 3410
M[5,]%*%M[,3]
[,1]
[1,] 13575
M[9,]%*%M[,5]
[,1]
[1,] 25395
M[2,]%*%M[,10]
[,1]
[1,] 45710
M[3,]%*%M[,4]
[,1]
[1,] 17865
M[3,]%*%M[,6]
[,1]
[1,] 27465
M[3,]%*%M[,8]
[,1]
[1,] 37065
M[3,]%*%M[,10]
[,1]
[1,] 46665
M[4,]%*%M[,5]
[,1]
[1,] 23120
M[4,]%*%M[,8]
[,1]
[1,] 37820
M[4,]%*%M[,9]
[,1]
[1,] 42720
M[4,]%*%M[,10]
[,1]
[1,] 47620
M[5,]%*%M[,5]
[,1]
[1,] 23575
M[5,]%*%M[,8]
[,1]
[1,] 38575
M[5,]%*%M[,10]
[,1]
[1,] 48575
M[5,]%*%M[,1]
[,1]
[1,] 3575
M[6,]%*%M[,6]
[,1]
[1,] 29130
M[6,]%*%M[,10]
[,1]
[1,] 49530
M[6,]%*%M[,3]
[,1]
[1,] 13830
M[6,]%*%M[,1]
[,1]
[1,] 3630
M[7,]%*%M[,7]
[,1]
[1,] 34885
M[7,]%*%M[,10]
[,1]
[1,] 50485
M[7,]%*%M[,9]
[,1]
[1,] 45285
M[7,]%*%M[,2]
[,1]
[1,] 8885
M[8,]%*%M[,3]
[,1]
[1,] 14340
M[8,]%*%M[,4]
[,1]
[1,] 19640
M[8,]%*%M[,8]
[,1]
[1,] 40840
M[8,]%*%M[,10]
[,1]
[1,] 51440
M[,1]%*%M[9,]
[,1]
[1,] 3795
M[,1]%*%M[5,]
[,1]
[1,] 3575
M[,2]%*%M[5,]
[,1]
[1,] 8575
M[,4]%*%M[10,]
[,1]
[1,] 20350
M[,5]%*%M[9,]
[,1]
[1,] 25395
Published on 06-Nov-2020 13:33:00