Matrix multiplication (*) in MATLAB - Time & Space Complexity
Matrix multiplication is a common operation in many programs. Understanding how its time grows helps us write faster code.
We want to know how the work needed changes as the matrix size gets bigger.
Analyze the time complexity of the following code snippet.
A = rand(n, n);
B = rand(n, n);
C = A * B;
This code multiplies two square matrices of size n by n using MATLAB's built-in operator.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying and adding elements for each entry in the result matrix.
- How many times: For each of the n rows and n columns, it sums n products, so about n x n x n = n³ times.
As the matrix size n grows, the number of operations grows much faster.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1,000 |
| 100 | 1,000,000 |
| 1000 | 1,000,000,000 |
Pattern observation: When n increases ten times, the work increases about a thousand times.
Time Complexity: O(n^3)
This means the time needed grows roughly with the cube of the matrix size.
[X] Wrong: "Matrix multiplication takes only n² steps because the result is an n by n matrix."
[OK] Correct: Each element in the result needs n multiplications and additions, so the total work is much more than just filling the matrix.
Knowing how matrix multiplication scales helps you understand performance in many areas like graphics, data science, and simulations. It shows you how to think about efficiency in real problems.
"What if we multiply a matrix of size n by another matrix of size n by m? How would the time complexity change?"
Practice
Solution
Step 1: Understand matrix multiplication size rules
Matrix multiplication requires the number of columns in the first matrix to match the number of rows in the second matrix.Step 2: Identify correct operator and output size
The operator for matrix multiplication is*, not.*, and the output size depends on the rows of the first and columns of the second matrix.Final Answer:
The number of columns in the first matrix must equal the number of rows in the second matrix. -> Option BQuick Check:
Matrix size rule = The number of columns in the first matrix must equal the number of rows in the second matrix. [OK]
- Confusing element-wise (.*) with matrix multiplication (*)
- Ignoring matrix size compatibility
- Assuming output size equals first matrix size
A by matrix B?Solution
Step 1: Identify the correct operator for matrix multiplication
In MATLAB,*is used for matrix multiplication, while.*is for element-wise multiplication.Step 2: Confirm the correct syntax
The commandC = A * B;correctly multiplies matrices A and B.Final Answer:
C = A * B; -> Option AQuick Check:
Matrix multiply uses * operator [OK]
- Using .* instead of * for matrix multiplication
- Using + or / operators incorrectly for multiplication
- Syntax errors with missing semicolons
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A * B;
Solution
Step 1: Multiply rows of A by columns of B
Calculate each element: C(1,1) = 1*5 + 2*7 = 19; C(1,2) = 1*6 + 2*8 = 22; C(2,1) = 3*5 + 4*7 = 43; C(2,2) = 3*6 + 4*8 = 50.Step 2: Form the resulting matrix
The resulting matrix C is [[19 22]; [43 50]].Final Answer:
[[19 22]; [43 50]] -> Option DQuick Check:
Matrix multiply result = [[19 22]; [43 50]] [OK]
- Adding matrices instead of multiplying
- Using element-wise multiplication results
- Mixing up row and column indices
A = [1 2 3; 4 5 6];
B = [7 8; 9 10];
C = A * B;
Solution
Step 1: Check matrix sizes for multiplication
A is 2x3, B is 2x2. Number of columns in A (3) does not equal number of rows in B (2), so multiplication is invalid.Step 2: Fix the error by adjusting B's size
To multiply, B must have 3 rows. For example, B could be 3x2 matrix.Final Answer:
Error: Matrix dimensions must agree. Fix by making B a 3xN matrix. -> Option CQuick Check:
Matrix size mismatch error = Error: Matrix dimensions must agree. Fix by making B a 3xN matrix. [OK]
- Ignoring dimension mismatch errors
- Trying to use .* instead of * to fix size errors
- Assuming multiplication works for any sizes
A = [1 0 2; -1 3 1];
B = [3 1; 2 1; 1 0];
Calculate the product
C = A * B and identify the element at position (2,1) in matrix C.Solution
Step 1: Verify matrix sizes for multiplication
A is 2x3, B is 3x2, so multiplication is valid and result C will be 2x2.Step 2: Calculate element C(2,1)
Multiply row 2 of A by column 1 of B: (-1)*3 + 3*2 + 1*1 = -3 + 6 + 1 = 4.Step 3: Recalculate carefully
Recheck calculation: (-1)*3 = -3, 3*2 = 6, 1*1 = 1; sum = 4.Final Answer:
4 -> Option AQuick Check:
Sum products for C(2,1) = 4 [OK]
- Mixing row and column indices
- Forgetting negative signs in multiplication
- Adding wrong elements or missing terms
Ready to prove it?
Pass the test and this pattern moves unknown
