0
0
R Programmingprogramming~20 mins

Matrix multiplication (%*%) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of matrix multiplication with compatible dimensions
What is the output of the following R code using matrix multiplication (%*%)?
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow=TRUE)
B <- matrix(c(2, 0, 1, 3), nrow=2, ncol=2, byrow=TRUE)
C <- A %*% B
print(C)
A
41 11 ],2[
6 4 ],1[
]2,[ ]1,[
B
[,1] [,2]
[1,] 4 8
[2,] 10 12
C
[,1] [,2]
[1,] 4 6
[2,] 11 14
D
[,1] [,2]
[1,] 5 6
[2,] 10 12
Attempts:
2 left
💡 Hint
Remember matrix multiplication sums the products of rows of the first matrix with columns of the second.
Predict Output
intermediate
2:00remaining
Result of multiplying a matrix by a vector
What is the output of this R code multiplying a matrix by a vector using %*%?
R Programming
M <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3, byrow=TRUE)
v <- c(1, 0, -1)
result <- M %*% v
print(result)
A
     [,1]
[1,]    2
[2,]    2
B
     [,1]
[1,]   -2
[2,]   -2
C
     [,1]
[1,]   -2
[2,]   -4
D
     [,1]
[1,]   -2
[2,]   -3
Attempts:
2 left
💡 Hint
Multiply each row of the matrix by the vector elements and sum.
Predict Output
advanced
2:00remaining
Output of matrix multiplication with incompatible dimensions
What happens when you try to multiply these two matrices with %*% in R?
R Programming
X <- matrix(1:6, nrow=2, ncol=3)
Y <- matrix(1:4, nrow=2, ncol=2)
Z <- X %*% Y
A
[,1] [,2]
[1,] 9 12
[2,] 19 26
BNULL
C[1] 9 12 19 26
DError in X %*% Y : non-conformable arguments
Attempts:
2 left
💡 Hint
Check if the number of columns in the first matrix equals the number of rows in the second.
🧠 Conceptual
advanced
1:30remaining
Understanding the dimensions of the product matrix
If matrix A is 4x3 and matrix B is 3x5, what will be the dimensions of A %*% B in R?
A4 rows and 5 columns
B3 rows and 3 columns
C5 rows and 4 columns
D4 rows and 3 columns
Attempts:
2 left
💡 Hint
The resulting matrix has the number of rows of the first and columns of the second.
Predict Output
expert
3:00remaining
Output of chained matrix multiplication with transposes
What is the output of this R code involving chained matrix multiplication and transpose?
R Programming
P <- matrix(c(1, 2, 3, 4), nrow=2)
Q <- matrix(c(0, 1, 1, 0), nrow=2)
R <- t(P) %*% Q %*% P
print(R)
A
[,1] [,2]
[1,] 4 10
[2,] 10 24
B
[,1] [,2]
[1,] 5 11
[2,] 11 25
C
[,1] [,2]
[1,] 7 13
[2,] 13 29
D
[,1] [,2]
[1,] 6 12
[2,] 12 26
Attempts:
2 left
💡 Hint
Calculate t(P), then multiply by Q, then multiply by P, carefully following matrix multiplication rules.