Bird
Raised Fist0
R Programmingprogramming~5 mins

Matrix multiplication (%*%) in R Programming - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the operator %*% do in R?
The operator %*% performs matrix multiplication between two matrices or vectors in R. It multiplies rows of the first matrix by columns of the second matrix and sums the products.
Click to reveal answer
beginner
Given matrices A (2x3) and B (3x2), what will be the dimension of A %*% B?
The result will be a 2x2 matrix because the number of rows comes from A and the number of columns comes from B.
Click to reveal answer
intermediate
Why can't you multiply two matrices with %*% if the number of columns in the first doesn't equal the number of rows in the second?
Because matrix multiplication requires each element in a row of the first matrix to multiply with the corresponding element in a column of the second matrix. If sizes don't match, this pairing is impossible.
Click to reveal answer
beginner
How is matrix multiplication different from element-wise multiplication in R?
Matrix multiplication (%*%) multiplies rows by columns and sums products, producing a new matrix. Element-wise multiplication (*) multiplies corresponding elements directly without summing.
Click to reveal answer
beginner
What will be the output of this R code?
A <- matrix(c(1,2,3,4), nrow=2)
B <- matrix(c(5,6,7,8), nrow=2)
A %*% B
The output will be a 2x2 matrix:
     [,1] [,2]
[1,] 19 22
[2,] 43 50

Explanation: Each element is the sum of products of rows of A and columns of B.
Click to reveal answer
What does the %*% operator do in R?
APerforms element-wise multiplication
BPerforms matrix multiplication
CCalculates the determinant of a matrix
DTransposes a matrix
If A is 3x2 and B is 2x4, what is the dimension of A %*% B?
A3x4
B2x2
C3x2
D2x4
Which condition must be true to multiply matrices A and B with %*%?
ANumber of columns in A equals number of rows in B
BNumber of rows in A equals number of rows in B
CNumber of columns in A equals number of columns in B
DNumber of rows in A equals number of columns in B
What is the difference between * and %*% in R?
ABoth do the same thing
B* is matrix multiplication; %*% is element-wise multiplication
C* is element-wise multiplication; %*% is matrix multiplication
D* transposes a matrix; %*% multiplies matrices
What will happen if you try to multiply a 2x3 matrix by a 4x2 matrix using %*%?
AIt will perform element-wise multiplication
BIt will produce a 2x2 matrix
CIt will produce a 4x3 matrix
DError due to incompatible dimensions
Explain how matrix multiplication works with the %*% operator in R.
Think about how each element in the result is calculated.
You got /3 concepts.
    Describe the difference between element-wise multiplication (*) and matrix multiplication (%*%) in R.
    Consider how the two operators treat the input matrices.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the operator %*% do in R?
      easy
      A. It multiplies two matrices using matrix multiplication rules.
      B. It adds two matrices element-wise.
      C. It multiplies two numbers element-wise.
      D. It transposes a matrix.

      Solution

      1. Step 1: Understand the operator %*%

        The operator %*% in R is specifically used for matrix multiplication, not element-wise operations or addition.
      2. Step 2: Differentiate from other operations

        Element-wise multiplication uses * and addition uses +. Transpose uses t(). So %*% is for matrix multiplication.
      3. Final Answer:

        It multiplies two matrices using matrix multiplication 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 %*% adds matrices
      • Using %*% to transpose matrices
      2. Which of the following is the correct syntax to multiply two matrices A and B in R?
      easy
      A. A * B
      B. A %/% B
      C. A + B
      D. A %*% B

      Solution

      1. Step 1: Identify matrix multiplication syntax

        In R, matrix multiplication uses the operator %*% between two matrices.
      2. Step 2: Check other operators

        * is element-wise multiplication, + is addition, and %/% is integer division, so they are incorrect for matrix multiplication.
      3. Final Answer:

        A %*% B -> Option D
      4. Quick Check:

        Matrix multiply = A %*% B [OK]
      Hint: Use %*% for matrix multiply, * is element-wise [OK]
      Common Mistakes:
      • Using * instead of %*% for matrix multiplication
      • Using + or %/% operators mistakenly
      • Forgetting spaces around %*%
      3. What is the result of the following R code?
      A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
      B <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2)
      C <- A %*% B
      print(C)
      medium
      A. [[6, 8], [10, 12]]
      B. [[5, 12], [21, 32]]
      C. [[23, 31], [34, 46]]
      D. [[12, 14], [16, 18]]

      Solution

      1. Step 1: Construct matrices A and B (column-major order)

        A = [[1, 3], [2, 4]]
        B = [[5, 7], [6, 8]]
      2. Step 2: Compute matrix multiplication

        C[1,1] = 1*5 + 3*6 = 5 + 18 = 23
        C[1,2] = 1*7 + 3*8 = 7 + 24 = 31
        C[2,1] = 2*5 + 4*6 = 10 + 24 = 34
        C[2,2] = 2*7 + 4*8 = 14 + 32 = 46
      3. Final Answer:

        [[23, 31], [34, 46]] -> Option C
      4. Quick Check:

        Matrix multiply result = [[23,31],[34,46]] [OK]
      Hint: Remember R fills matrices by column, not row [OK]
      Common Mistakes:
      • Assuming row-wise filling of matrices
      • Mixing element-wise and matrix multiplication
      • Incorrect multiplication order
      4. Consider the code below. What is the error and how to fix it?
      A <- matrix(1:6, nrow=2, ncol=3)
      B <- matrix(1:4, nrow=2, ncol=2)
      C <- A %*% B
      medium
      A. Error: Syntax error; fix by adding commas in matrix() calls.
      B. Error: Non-conformable arguments; fix by making A's columns equal B's rows.
      C. No error; code runs fine and returns a 2x2 matrix.
      D. Error: Cannot multiply matrices with different number of rows; fix by transposing B.

      Solution

      1. Step 1: Check matrix dimensions

        A is 2x3, B is 2x2. For matrix multiplication, A's columns (3) must equal B's rows (2). They don't match.
      2. Step 2: Identify error and fix

        R will give 'non-conformable arguments' error. To fix, B must have 3 rows or A must have 2 columns. For example, B <- matrix(1:6, nrow=3, ncol=2).
      3. Final Answer:

        Error: Non-conformable arguments; fix by making A's columns equal B's rows. -> Option B
      4. Quick Check:

        Matrix dims must match inner dimensions [OK]
      Hint: Check if A's columns = B's rows before multiplying [OK]
      Common Mistakes:
      • Ignoring dimension mismatch
      • Trying to multiply incompatible matrices
      • Assuming R auto-adjusts matrix sizes
      5. You have two matrices:
      M1 <- matrix(c(2, 0, 1, 3, 4, 5), nrow=2, ncol=3)
      M2 <- matrix(c(1, 2, 3, 4, 5, 6), nrow=3, ncol=2)

      What is the resulting matrix when you compute M1 %*% M2?
      hard
      A. [[16, 37], [21, 45]]
      B. [[8, 12], [39, 54]]
      C. [[5, 10], [29, 40]]
      D. [[2, 4], [3, 6]]

      Solution

      1. Step 1: Construct matrices M1 and M2 (column-major order)

        M1 = [[2, 1, 4], [0, 3, 5]]
        M2 = [[1, 4], [2, 5], [3, 6]]
      2. Step 2: Compute each element of the product

        C[1,1] = 2*1 + 1*2 + 4*3 = 2 + 2 + 12 = 16
        C[1,2] = 2*4 + 1*5 + 4*6 = 8 + 5 + 24 = 37
        C[2,1] = 0*1 + 3*2 + 5*3 = 0 + 6 + 15 = 21
        C[2,2] = 0*4 + 3*5 + 5*6 = 0 + 15 + 30 = 45
      3. Final Answer:

        [[16, 37], [21, 45]] -> Option A
      4. Quick Check:

        Matrix multiply result = [[16,37],[21,45]] [OK]
      Hint: Remember R fills matrices by column, check carefully [OK]
      Common Mistakes:
      • Assuming row-wise filling instead of column-wise
      • Mixing element-wise and matrix multiplication
      • Misaligning matrix dimensions
      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