Complete the code to create a 2x2 matrix with values 1 to 4.
mat <- matrix(1:4, nrow = [1], ncol = 2)
The matrix has 2 rows and 2 columns, so nrow = 2 is correct.
Complete the code to add two matrices A and B.
result <- [1] + Bsum which sums all elements instead of element-wise addition.matrix keyword incorrectly.To add two matrices, use the + operator between them. Here, A + B.
Fix the error in the code to multiply matrices X and Y.
product <- X [1] Y* which does element-wise multiplication.%% or %.Matrix multiplication in R uses the operator %*%, not *.
Fill both blanks to create a matrix M with 3 rows and 4 columns filled by row.
M <- matrix(1:12, nrow = [1], ncol = [2], byrow = TRUE)
byrow to FALSE by mistake.The matrix has 3 rows and 4 columns, so nrow = 3 and ncol = 4.
Fill all three blanks to create a named matrix mat with row and column names.
mat <- matrix(1:6, nrow = [1], ncol = [2], dimnames = list([3], c("A", "B")))
The matrix has 3 rows and 2 columns, so nrow = 3 and ncol = 2. The row names vector must match the number of rows, so c("Row1", "Row2", "Row3") is correct.
