Math and Stats: Exercises


6. Matrix addition. You are given two matrices: X, Y, and Z. You can check the dimensions of each object in R with the dim() function.

In order to perform matrix addition, the dimensions of the two matrices must be identical. Figure out which matrix can be added to X and add it. You can add two matrices using the + operator.


X <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0), nrow=3, ncol=4) Y <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, ncol=3) Z <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=3, ncol=4) # Compute the dimensions of X. ___(X) # Compute the dimensions of Y. ___ # Compute the dimensions of Z. ___ # Add the correct matrix to X. X ___ ___ # Compute the dimensions of X. dim(X) # Compute the dimensions of Y. dim(Y) # Compute the dimensions of Z. dim(Z) # Add the correct matrix to X. X + Z test_error() test_function("dim", incorrect_msg="Did you use the `dim()` function?") success_msg("Excellent work. Notice that X+Z = Z. What type of matrix is X?")
You can use the dim() function to check a matrix's dimensions.

Previous Exercise Next Exercise