Matrix Algebra: Exercises


3. Matrix multiplication. Four matrices have been defined for you: A, B, C, and D. Use dim() to calculate the dimensions of each matrix.

After you have identified each matrix's dimensions, post-multiply A by the matrix that has conforming dimensions. After that, pre-multiply A by the matrix that has conforming dimensions. You can use the operator %*% to perform matrix multiplication in R.


A <- matrix(1:30, nrow = 6, ncol = 5) B <- matrix(1:30, nrow = 5, ncol = 4) C <- matrix(1:30, nrow = 3, ncol = 6) D <- matrix(1:21, nrow = 3, ncol = 7) # Print dimensions of A. print(dim(A)) # Print dimensions of B. print(dim(___)) # Print dimensions of C. print(___) # Print dimensions of D. ___ # Post-multiply A by the conforming matrix. A %*% ___ # Pre-multiply A by the conforming matrix. ___ %*% A # Print dimensions of A. print(dim(A)) # Print dimensions of B. print(dim(B)) # Print dimensions of C. print(dim(C)) # Print dimensions of D. print(dim(D)) # Post-multiply A by the conforming matrix. A %*% B # Pre-multiply A by the conforming matrix. C %*% A test_error() success_msg("Good work. What has to be true about two matrices for the conformability requirement to be satisfied for matrix multiplication?")
Recall that the matrix() function can specify a number of rows and a number of columns.

Previous Exercise Next Exercise