Bird
Raised Fist0
R Programmingprogramming~20 mins

Matrix arithmetic in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Matrix multiplication output
What is the output of this R code when multiplying two matrices?
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2)
B <- matrix(c(2, 0, 1, 3), nrow=2)
C <- A %*% B
print(C)
A
[,1] [,2]
[1,] 4 6
[2,] 11 12
B
[,1] [,2]
[1,] 4 9
[2,] 10 12
C
[,1] [,2]
[1,] 5 6
[2,] 10 12
D
21 01 ],2[
9 4 ],1[
]2,[ ]1,[
Attempts:
2 left
💡 Hint
Remember matrix multiplication multiplies rows of the first by columns of the second.
❓ Predict Output
intermediate
2:00remaining
Element-wise multiplication result
What does this R code output when performing element-wise multiplication of two matrices?
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2)
B <- matrix(c(2, 0, 1, 3), nrow=2)
C <- A * B
print(C)
A
[,1] [,2]
[1,] 2 0
[2,] 1 12
B
21 3 ],2[
0 2 ],1[
]2,[ ]1,[
C
,1] [,2]
[1,] 2 0
[2,] 3 12
D
[,1] [,2]
[1,] 2 0
[2,] 3 12
Attempts:
2 left
💡 Hint
Element-wise multiplication multiplies corresponding elements directly.
❓ Predict Output
advanced
2:00remaining
Matrix inversion and multiplication
What is the output of this R code that multiplies a matrix by its inverse?
R Programming
M <- matrix(c(4, 7, 2, 6), nrow=2)
invM <- solve(M)
result <- M %*% invM
print(round(result, 2))
A
[,1] [,2]
[1,] 1 0
[2,] 0 1
B
[,1] [,2]
[1,] 4 7
[2,] 2 6
C
[,1] [,2]
[1,] 0.5 0
[2,] 0 0.5
D
[,1] [,2]
[1,] 1 1
[2,] 1 1
Attempts:
2 left
💡 Hint
Multiplying a matrix by its inverse should give the identity matrix.
❓ Predict Output
advanced
2:00remaining
Matrix transpose and multiplication
What is the output of this R code that multiplies a matrix by its transpose?
R Programming
X <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2)
Y <- X %*% t(X)
print(Y)
A
[,1] [,2]
[1,] 14 32
[2,] 32 38
B
[,1] [,2]
[1,] 14 32
[2,] 32 50
C
[,1] [,2]
[1,] 35 44
[2,] 44 56
D
[,1] [,2]
[1,] 14 20
[2,] 20 77
Attempts:
2 left
💡 Hint
Multiply each row of X by each row of X again because of transpose.
🧠 Conceptual
expert
2:00remaining
Matrix rank and multiplication properties
Given two matrices A (3x2) and B (2x3), which statement about the rank of the product matrix C = A %*% B is always true?
Arank(C) ≤ min(rank(A), rank(B))
Brank(C) = rank(A) + rank(B)
Crank(C) ≥ max(rank(A), rank(B))
Drank(C) = rank(A) * rank(B)
Attempts:
2 left
💡 Hint
The rank of a product cannot exceed the ranks of the factors.

Practice

(1/5)
1. In R, what does the operator %*% do when applied between two matrices?
easy
A. Performs matrix multiplication following linear algebra rules
B. Adds the two matrices element-wise
C. Subtracts the second matrix from the first element-wise
D. Multiplies the two matrices element-wise

Solution

  1. Step 1: Understand matrix operators in R

    In R, + and - perform element-wise addition and subtraction respectively, while %*% is used for matrix multiplication.
  2. Step 2: Identify the role of %*%

    The operator %*% multiplies matrices according to linear algebra rules, not element-wise.
  3. Final Answer:

    Performs matrix multiplication following linear algebra rules -> Option A
  4. Quick Check:

    %*% = matrix multiplication [OK]
Hint: Remember: %*% means matrix multiplication, not element-wise [OK]
Common Mistakes:
  • Confusing %*% with element-wise multiplication
  • Thinking + or - does matrix multiplication
  • Using * for matrix multiplication
2. Which of the following is the correct way to create a 2x2 matrix with values 1, 2, 3, 4 in R?
easy
A. matrix(1, 2, 3, 4, 2, 2)
B. matrix(1, 2, 3, 4, nrow=2, ncol=2)
C. matrix(c(1, 2, 3, 4), rows=2, cols=2)
D. matrix(c(1, 2, 3, 4), nrow=2, ncol=2)

Solution

  1. Step 1: Recall matrix creation syntax in R

    The function matrix() takes a vector of values and parameters nrow and ncol to specify dimensions.
  2. Step 2: Check each option

    matrix(c(1, 2, 3, 4), nrow=2, ncol=2) correctly uses c(1, 2, 3, 4) and named arguments nrow=2, ncol=2. matrix(1, 2, 3, 4, nrow=2, ncol=2) has wrong argument format. matrix(c(1, 2, 3, 4), rows=2, cols=2) uses invalid argument names. matrix(1, 2, 3, 4, 2, 2) uses positional arguments without the vector wrapper.
  3. Final Answer:

    matrix(c(1, 2, 3, 4), nrow=2, ncol=2) -> Option D
  4. Quick Check:

    Use c() vector and nrow, ncol named args [OK]
Hint: Always wrap values in c() and name nrow, ncol [OK]
Common Mistakes:
  • Not using c() to combine values
  • Using wrong argument names like rows or cols
  • Passing values without vector wrapper
3. What is the output of the following R code?
m1 <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow=TRUE)
m2 <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2, byrow=TRUE)
m3 <- m1 + m2
print(m3)
medium
A. [1 2; 3 4]
B. Error: non-conformable arrays
C. [6 8; 10 12]
D. [5 6; 7 8]

Solution

  1. Step 1: Understand element-wise addition

    Using + between matrices adds corresponding elements: (1+5), (2+6), (3+7), (4+8).
  2. Step 2: Calculate each element

    Results: 6, 8, 10, 12 arranged in 2x2 matrix.
  3. Final Answer:

    [6 8; 10 12] -> Option C
  4. Quick Check:

    Element-wise addition = sum of each element pair [OK]
Hint: Use + for element-wise addition, add each element pair [OK]
Common Mistakes:
  • Expecting matrix multiplication instead of addition
  • Confusing matrix dimensions causing errors
  • Misreading matrix layout in output
4. Consider this R code snippet:
m1 <- matrix(c(1, 2, 3, 4), nrow=2)
m2 <- matrix(c(5, 6, 7), nrow=3)
m3 <- m1 %*% m2
print(m3)

What is the problem with this code?
medium
A. The matrices have incompatible dimensions for multiplication
B. The matrix m1 is not square
C. The operator %*% is invalid for matrices
D. The matrix m2 has missing values

Solution

  1. Step 1: Check matrix dimensions

    m1 is 2x2, m2 is 3x1 (since 3 values and nrow=3). For multiplication, columns of m1 (2) must equal rows of m2 (3).
  2. Step 2: Identify dimension mismatch

    2 != 3, so multiplication is not possible, causing an error.
  3. Final Answer:

    The matrices have incompatible dimensions for multiplication -> Option A
  4. Quick Check:

    Matrix multiplication needs matching inner dimensions [OK]
Hint: Check if m1 columns = m2 rows before multiplying [OK]
Common Mistakes:
  • Assuming any matrices can be multiplied
  • Ignoring dimension mismatch errors
  • Thinking %*% works element-wise
5. You have two matrices in R:
m1 <- matrix(c(1, 0, 2, -1), nrow=2)
m2 <- matrix(c(3, 1, 4, 2), nrow=2)

You want to compute the matrix product m1 %*% m2 and then subtract the identity matrix of size 2. Which code correctly does this?
hard
A. m3 <- m1 * m2 - diag(2)
B. m3 <- m1 %*% m2 - diag(2)
C. m3 <- m1 %*% m2 - matrix(1, 2, 2)
D. m3 <- m1 %*% m2 - identity(2)

Solution

  1. Step 1: Compute matrix multiplication

    Use %*% to multiply m1 and m2 correctly.
  2. Step 2: Subtract identity matrix

    Use diag(2) to create a 2x2 identity matrix and subtract it from the product.
  3. Final Answer:

    m3 <- m1 %*% m2 - diag(2) -> Option B
  4. Quick Check:

    Use diag() for identity matrix in subtraction [OK]
Hint: Use diag(n) for identity matrix, %*% for multiplication [OK]
Common Mistakes:
  • Using * instead of %*% for multiplication
  • Using matrix(1,2,2) which is all ones, not identity
  • Using identity() which is not an R base function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard