Matrix multiplication helps combine two sets of numbers arranged in rows and columns to get a new set of numbers. It is useful for solving problems involving grids or tables of numbers.
When you want to combine two tables of numbers where the number of columns in the first matches the number of rows in the second.
When calculating transformations in graphics or geometry, like rotating or scaling shapes.
When working with data in statistics or machine learning that involves multiplying features by weights.
When solving systems of linear equations using matrices.
When combining multiple steps of calculations that can be represented as matrices.
Syntax
R Programming
result <- matrix1 %*% matrix2
The number of columns in matrix1 must be the same as the number of rows in matrix2.
The result will be a new matrix with rows from matrix1 and columns from matrix2.
Examples
Multiply two 2x2 matrices A and B to get matrix C.
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2, byrow=TRUE)
B <- matrix(c(5, 6, 7, 8), nrow=2, byrow=TRUE)
C <- A %*% B
Multiply a 2x3 matrix X by a 3x1 matrix Y to get a 2x1 matrix Z.
R Programming
X <- matrix(1:6, nrow=2)
Y <- matrix(7:9, nrow=3)
Z <- X %*% Y
Sample Program
This program creates two 2x2 matrices A and B, multiplies them using %*%, and prints the result matrix C.
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2, byrow=TRUE)
B <- matrix(c(5, 6, 7, 8), nrow=2, byrow=TRUE)
C <- A %*% B
print(C)
OutputSuccess
Important Notes
Matrix multiplication is not the same as element-wise multiplication (which uses * in R).
If the dimensions do not match, R will give an error.
Summary
Use %*% to multiply matrices when the inner dimensions match.
The result matrix has the outer dimensions of the two matrices.
This operation is common in math, data science, and graphics.
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
Step 1: Understand the operator %*%
The operator %*% in R is specifically used for matrix multiplication, not element-wise operations or addition.
Step 2: Differentiate from other operations
Element-wise multiplication uses * and addition uses +. Transpose uses t(). So %*% is for matrix multiplication.
Final Answer:
It multiplies two matrices using matrix multiplication rules. -> Option A
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
Step 1: Identify matrix multiplication syntax
In R, matrix multiplication uses the operator %*% between two matrices.
Step 2: Check other operators
* is element-wise multiplication, + is addition, and %/% is integer division, so they are incorrect for matrix multiplication.
Final Answer:
A %*% B -> Option D
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
Step 1: Construct matrices A and B (column-major order)