Special matrices (zeros, ones, eye, rand) in MATLAB - Time & Space Complexity
When creating special matrices like zeros, ones, eye, or rand in MATLAB, it's important to know how the time to create them grows as the matrix size increases.
We want to understand how the work done changes when the matrix gets bigger.
Analyze the time complexity of the following code snippet.
n = 1000;
A = zeros(n, n);
B = ones(n, n);
C = eye(n);
D = rand(n, n);
This code creates four special matrices of size n by n: all zeros, all ones, identity, and random values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filling each element of the n by n matrix.
- How many times: Each matrix has n * n elements, so the operation repeats n squared times.
As the matrix size n grows, the number of elements grows by n squared, so the work grows quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: Doubling n makes the work about four times bigger because the matrix has n squared elements.
Time Complexity: O(n^2)
This means the time to create these matrices grows proportionally to the square of the matrix size.
[X] Wrong: "Creating these matrices takes time proportional to n, not n squared."
[OK] Correct: Each matrix has n rows and n columns, so the total elements are n times n, which is n squared. The computer must set each element, so the time grows with n squared.
Understanding how matrix creation time grows helps you reason about performance in data processing and simulations, which is a useful skill in many programming tasks.
"What if we create a vector instead of a matrix, like zeros(n, 1)? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function purpose
Thezerosfunction creates a matrix where every element is zero.Step 2: Compare with other functions
onescreates all ones,eyecreates identity matrix,randcreates random values.Final Answer:
zeros -> Option AQuick Check:
zeros = zeros matrix [OK]
- Confusing zeros with ones
- Thinking eye creates zeros
- Using rand for zeros
Solution
Step 1: Recall identity matrix creation
Theeye(n)function creates an n-by-n identity matrix with ones on the diagonal.Step 2: Check syntax correctness
eye(3)correctly creates a 3x3 identity matrix. Others create different matrices.Final Answer:
eye(3) -> Option AQuick Check:
eye(3) = 3x3 identity [OK]
- Using ones or zeros instead of eye
- Missing parentheses
- Using rand for identity
A = ones(2,3); B = zeros(2,3); C = A + B;
Solution
Step 1: Understand matrix creation
ones(2,3)creates a 2x3 matrix of all ones,zeros(2,3)creates a 2x3 matrix of zeros.Step 2: Add matrices element-wise
Adding ones and zeros element-wise results in the same ones matrix.Final Answer:
[[1 1 1]; [1 1 1]] -> Option CQuick Check:
ones + zeros = ones [OK]
- Thinking addition doubles values
- Confusing matrix sizes
- Expecting error on addition
M = zeros(3); M(2,4) = 5;
Solution
Step 1: Check matrix size
zeros(3)creates a 3x3 matrix, so valid indices are 1 to 3 for both rows and columns.Step 2: Analyze indexing
AccessingM(2,4)tries to access column 4, which does not exist, causing an index error.Final Answer:
Index exceeds matrix dimensions error -> Option BQuick Check:
Index out of range = error [OK]
- Ignoring matrix size limits
- Thinking semicolon causes error
- Assuming zeros() creates bigger matrix
Solution
Step 1: Understand the goal
Create a 4x4 matrix with random values, then set diagonal elements to 1.Step 2: Analyze each option
M = rand(4); M(eye(4) == 1) = 1;creates random 4x4 matrix, then replaces diagonal elements (where eye(4)==1) with 1, matching the requirement.Final Answer:
M = rand(4); M(eye(4) == 1) = 1; -> Option DQuick Check:
Random matrix with diagonal ones =M = rand(4); M(eye(4) == 1) = 1;[OK]
- Adding matrices incorrectly
- Replacing wrong elements
- Using ones() instead of random values
Ready to prove it?
Pass the test and this pattern moves unknown
