Complete the code to create a 2x2 matrix with numbers 1 to 4.
mat <- matrix([1], nrow = 2, ncol = 2)
The matrix() function needs a vector of values. 1:4 creates numbers 1 to 4, perfect for a 2x2 matrix.
Complete the code to access the element in the first row and second column of the matrix.
value <- mat[[1] , 2]
Matrix rows and columns start at 1 in R. To get the first row, use 1.
Fix the error in the code to multiply two matrices correctly.
result <- mat1 [1] mat2Matrix multiplication in R uses the operator %*%. The * operator does element-wise multiplication.
Fill both blanks to create a matrix with 3 rows and 2 columns using numbers 1 to 6.
mat <- matrix([1], nrow = [2], ncol = 2)
Use 1:6 for values and 3 for number of rows to get a 3x2 matrix.
Fill all three blanks to create a matrix from vector 'v', with 2 rows, 3 columns, and fill by row.
mat <- matrix([1], nrow = [2], ncol = [3], byrow = TRUE)
Use vector v, set rows to 2, columns to 3, and byrow = TRUE to fill matrix by rows.