Cell array creation in MATLAB - Time & Space Complexity
We want to understand how the time needed to create a cell array changes as the size grows.
How does the work increase when we make bigger cell arrays?
Analyze the time complexity of the following code snippet.
n = 1000;
C = cell(1, n);
for i = 1:n
C{i} = i^2;
end
This code creates a cell array of size n and fills each cell with the square of its index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 1 to n.
- How many times: Exactly n times, once for each cell assignment.
Each time we increase n, the loop runs more times, doing more assignments.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to create and fill the cell array grows in a straight line with the number of cells.
[X] Wrong: "Creating a cell array is instant and does not depend on size."
[OK] Correct: Even though the cell array structure is created quickly, filling each cell with data takes time proportional to the number of cells.
Understanding how loops affect time helps you explain and improve code that handles collections like cell arrays.
"What if we preallocate the cell array but fill only half of the cells? How would the time complexity change?"
Practice
Solution
Step 1: Understand cell array syntax
Cell arrays use curly braces { } to hold mixed data types.Step 2: Identify correct syntax for mixed types
myCell = {42, 'hello', true}; uses curly braces with different types: number, string, logical.Final Answer:
myCell = {42, 'hello', true}; -> Option CQuick Check:
Curly braces create cell arrays [OK]
- Using square brackets instead of curly braces
- Using parentheses instead of curly braces
- Using angle brackets which are invalid
Solution
Step 1: Recall cell array 2D syntax
Use curly braces { } with semicolons for rows and commas for columns.Step 2: Check each option
C = {1, 'a'; 3.14, false}; correctly uses { } with two rows and two columns separated by ; and ,.Final Answer:
C = {1, 'a'; 3.14, false}; -> Option BQuick Check:
Curly braces + semicolon for rows = correct 2D cell array [OK]
- Using square brackets which create numeric arrays
- Using commas instead of semicolons for rows
- Using parentheses which are invalid for arrays
myCell = {10, 'MATLAB'; 'false', 3.5};
disp(myCell{2,1});Solution
Step 1: Understand cell content access
Using curly braces with indices accesses the content inside the cell.Step 2: Identify the element at row 2, column 1
myCell{2,1} is the element in second row, first column, which is 'false' as a string.Final Answer:
'false' -> Option AQuick Check:
myCell{2,1} = 'false' [OK]
- Using parentheses instead of curly braces to access content
- Confusing row and column indices
- Expecting the whole cell instead of content
C = {1, 2, 3; 4, 5, 6};
val = C(2,3);What is the issue with this code if you want to get the number 6 as a value?
Solution
Step 1: Understand difference between () and {}
Parentheses () return a cell array subset, curly braces {} return content.Step 2: Analyze C(2,3)
C(2,3) returns a 1x1 cell containing 6, not the number 6 itself.Final Answer:
C(2,3) returns a cell, not the content inside the cell -> Option AQuick Check:
Use curly braces to get content: C{2,3} [OK]
- Using parentheses expecting content instead of cell
- Confusing cell array indexing with numeric array indexing
- Expecting direct numeric output from parentheses indexing
Solution
Step 1: Identify nested cell array syntax
To nest a cell array inside another, use curly braces { } inside the main cell array.Step 2: Check each option for correct nesting
nestedCell = {[1, 2, 3], 'text', {true, false}}; uses {true, false} as a nested cell array inside the main cell array.Final Answer:
nestedCell = {[1, 2, 3], 'text', {true, false}}; -> Option DQuick Check:
Curly braces inside curly braces create nested cells [OK]
- Using square brackets for nested cell arrays
- Using parentheses which are invalid
- Using semicolons inside nested cell arrays incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
