Math and Stats: Exercises


5. Matrix-vector multiplication. The matrix, X, and vector, Y, have been defined for you. You can check the dimensions of a matrix or vector in R with the dim() function.

In order to perform matrix-vector multiplication, the number of columns of the first matrix (or vector) must be equal to the number of rows of the second. Multiply the X and Y in the order that makes it a conformable operation using the %*% operation.


X <- matrix(c(1,0,0,0,1,0,0,0,1), nrow=3, ncol=3) Y <- matrix(c(1,2,3), nrow=1, ncol=3) # Compute the dimensions of X. ___(X) # Compute the dimensions of Y. ___ # Multiply X and Y in the correct order. ___ ___ ___ # Compute the dimensions of X. dim(X) # Compute the dimensions of Y. dim(Y) # Multiply X and Y in the correct order. Y %*% X test_error() test_function("dim", incorrect_msg="Did you use the `dim()` function?") success_msg("Excellent work. What happens if you switch the order of Y and X?")
You can use the dim() function to check a matrix's dimensions.

Previous Exercise Next Exercise