Matrix operations are also vectorized, making for nicely compact notation. This way, we can do element-by-element operations on matrices without having to loop over every element.
x <- matrix(3:6, 2, 2)
y <- matrix(rep(1:2, each = 2), 2, 2)
x
## [,1] [,2]
## [1,] 3 5
## [2,] 4 6
y
## [,1] [,2]
## [1,] 1 2
## [2,] 1 2
## element-wise multiplication
x * y
## [,1] [,2]
## [1,] 3 10
## [2,] 4 12
## element-wise division
x / y
## [,1] [,2]
## [1,] 3 2.5
## [2,] 4 3.0
## true matrix multiplication
z <- x %*% y
z
## [,1] [,2]
## [1,] 8 16
## [2,] 10 20
z11 <- x[1,] %*% y[,1]
z21 <- x[2,] %*% y[,1]
z11
## [,1]
## [1,] 8
z21
## [,1]
## [1,] 10
z12 <- x[1,] %*% y[,2]
z22 <- x[2,] %*% y[,2]
z12
## [,1]
## [1,] 16
z22
## [,1]
## [1,] 20