Matrix creation in MATLAB - Time & Space Complexity
When we create a matrix in MATLAB, the computer needs to set up space and fill values. We want to know how the time it takes changes as the matrix size grows.
How does the work grow when we make bigger matrices?
Analyze the time complexity of the following code snippet.
n = 1000;
A = zeros(n, n);
for i = 1:n
for j = 1:n
A(i,j) = i + j;
end
end
This code creates an n-by-n matrix and fills each element with the sum of its row and column indices.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filling each element of the matrix inside nested loops.
- How many times: The inner operation runs n x n times, once for each element.
As the matrix size n grows, the number of operations grows by the square of n because we fill every element in a two-dimensional grid.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: When n increases ten times, the work increases one hundred times.
Time Complexity: O(n^2)
This means the time to create and fill the matrix grows proportionally to the square of its size.
[X] Wrong: "Filling the matrix takes time proportional to n, not n squared."
[OK] Correct: Because the matrix has n rows and n columns, you must fill n x n elements, so the work grows with n squared, not just n.
Understanding how matrix creation time grows helps you explain efficiency in tasks like image processing or data tables, where big matrices are common. It shows you can think about how code scales with input size.
"What if we only filled the diagonal elements of the matrix? How would the time complexity change?"
Practice
Solution
Step 1: Understand matrix dimensions
A 2x3 matrix has 2 rows and 3 columns.Step 2: Check each option's shape
[1 2 3; 4 5 6]has 2 rows separated by semicolon and 3 columns separated by spaces, so it is 2x3.Final Answer:
[1 2 3; 4 5 6] -> Option CQuick Check:
2 rows and 3 columns = [1 2 3; 4 5 6] [OK]
- Using commas instead of semicolons for rows
- Mismatched number of columns in rows
- Confusing rows and columns
Solution
Step 1: Recall identity matrix function
In MATLAB,eye(n)creates an n-by-n identity matrix.Step 2: Check options
eye(3)useseye(3), which is the correct function for a 3x3 identity matrix.Final Answer:
eye(3) -> Option AQuick Check:
Identity matrix = eye(3) [OK]
- Using zeros or ones instead of eye
- Using diag with incorrect vector size
- Confusing identity with zero matrix
A = [1 2; 3 4];
B = zeros(2,3);
C = [A B];
disp(C)
Solution
Step 1: Understand matrix sizes
A is 2x2, B is 2x3 of zeros.Step 2: Concatenate horizontally
[A B] joins matrices side by side, resulting in 2 rows and 5 columns.Final Answer:
[1 2 0 0 0; 3 4 0 0 0] -> Option BQuick Check:
Horizontal concat of 2x2 and 2x3 = 2x5 matrix [OK]
- Trying to concat matrices with different row counts vertically
- Confusing zeros size
- Expecting error instead of valid concat
M = [1 2 3; 4 5];
Solution
Step 1: Check matrix row sizes
First row has 3 elements, second row has 2 elements.Step 2: Understand MATLAB matrix rules
All rows must have the same number of columns; here they differ.Final Answer:
Rows have different number of columns -> Option DQuick Check:
Matrix rows must match column count [OK]
- Thinking commas are required between rows
- Using parentheses instead of brackets
- Ignoring unequal row lengths
Solution
Step 1: Create a 4x4 matrix of 0.5
Using0.5*ones(4)sets all elements to 0.5.Step 2: Replace diagonal elements with 1
Using logical indexingM(eye(4) == 1) = 1;sets diagonal to 1.Final Answer:
M = 0.5*ones(4); M(eye(4) == 1) = 1; -> Option AQuick Check:
Diagonal 1, others 0.5 =M = 0.5*ones(4); M(eye(4) == 1) = 1;[OK]
- Adding eye(4) instead of replacing diagonal
- Mixing addition and multiplication incorrectly
- Setting diagonal to 0.5 instead of 1
Ready to prove it?
Pass the test and this pattern moves unknown
