C22 : Compute the product AB of the two matrices below using both the definition of the matrix-vector product and the definition of matrix multiplication.
A = matrix(c(1, 0, -2, 1), byrow = T, nrow = 2)
B = matrix(c(2, 3, 4, 6), byrow = T, nrow = 2)
#Multiplication using matrix-vector product
(AB.1 <- B[1,1] * A[,1] + B[2,1] * A[,2])
## [1] 2 0
(AB.2 <- B[1,2] * A[,1] + B[2,2] * A[,2])
## [1] 3 0
(AB <- cbind(AB.1, AB.2))
## AB.1 AB.2
## [1,] 2 3
## [2,] 0 0
#Multiplication using matrix multiplication
(AB.1.1 <- A[1,1] * B[1,1] + A[1,2] * B[2,1])
## [1] 2
(AB.2.1 <- A[2,1] * B[1,1] + A[2,2] * B[2,1])
## [1] 0
(AB.1.2 <- A[1,1] * B[1,2] + A[1,2] * B[2,2])
## [1] 3
(AB.2.2 <- A[2,1] * B[1,2] + A[2,2] * B[2,2])
## [1] 0
(AB <- matrix(c(AB.1.1, AB.2.1, AB.1.2, AB.2.2), nrow = 2, byrow = F))
## [,1] [,2]
## [1,] 2 3
## [2,] 0 0