Challenge - 5 Problems
Matrix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this matrix creation code?
Consider the following R code that creates a matrix. What will be the output when printed?
R Programming
m <- matrix(1:6, nrow=2, ncol=3) print(m)
Attempts:
2 left
💡 Hint
Remember that R fills matrices by columns by default.
✗ Incorrect
The matrix function fills the matrix column-wise by default. So the numbers 1 to 6 fill down the first column, then the second, then the third.
❓ Predict Output
intermediate2:00remaining
What is the value of element m[2,3] after this code?
Given the matrix creation below, what is the value stored at row 2, column 3?
R Programming
m <- matrix(seq(10, 60, by=10), nrow=3, ncol=2) m[2,3] <- 99 print(m)
Attempts:
2 left
💡 Hint
Check the dimensions of the matrix before assigning.
✗ Incorrect
The matrix has 3 rows and 2 columns, so column 3 does not exist. Trying to assign m[2,3] causes an error.
🧠 Conceptual
advanced2:00remaining
How does the byrow argument affect matrix creation?
What is the difference in the matrix created by these two commands?
m1 <- matrix(1:6, nrow=2, ncol=3)
m2 <- matrix(1:6, nrow=2, ncol=3, byrow=TRUE)
Choose the correct description.
Attempts:
2 left
💡 Hint
Check the default behavior of matrix filling and what byrow=TRUE changes.
✗ Incorrect
By default, matrix fills by columns. Setting byrow=TRUE makes it fill by rows instead.
🔧 Debug
advanced2:00remaining
What error does this matrix creation code produce?
Examine the code below and identify the error it produces when run in R:
m <- matrix(1:8, nrow=3, ncol=3)
print(m)
Attempts:
2 left
💡 Hint
Think about what happens when the data length does not match the matrix size exactly.
✗ Incorrect
R recycles the vector 1:8 to fill the 3x3 matrix (9 elements), so no error occurs.
🚀 Application
expert2:00remaining
How many elements are in the matrix created by this code?
What is the total number of elements in the matrix created by this R code?
m <- matrix(data=rep(5, times=12), nrow=4, ncol=3)
Attempts:
2 left
💡 Hint
Multiply the number of rows by the number of columns.
✗ Incorrect
A matrix with 4 rows and 3 columns has 4*3=12 elements.