Reshaping arrays in MATLAB - Time & Space Complexity
When we reshape arrays in MATLAB, we change their shape without changing the data.
We want to know how the time to reshape grows as the array size grows.
Analyze the time complexity of the following code snippet.
A = 1:1000; % Create a row vector with 1000 elements
B = reshape(A, 100, 10); % Reshape into a 100x10 matrix
This code reshapes a 1D array into a 2D matrix by rearranging elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating shape metadata (dimensions array). No element copying or traversal.
- How many times: Constant time, independent of array size (n).
As the number of elements grows, the time to reshape stays roughly constant.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 operations |
| 100 | About 5 operations |
| 1000 | About 5 operations |
Pattern observation: The time is constant regardless of the number of elements.
Time Complexity: O(1)
This means the time to reshape is constant and does not grow with the number of elements. MATLAB reshape updates metadata without copying data.
[OK] Correct thinking: "Reshaping is instant and does not depend on array size."
[X] Why the wrong one fails: Reshape does not copy or process elements; it only updates metadata, so no O(n) cost.
Understanding how reshaping scales helps you reason about data manipulation efficiency in real tasks.
"What if we reshape a very large array multiple times in a loop? How would the time complexity change?"
Practice
reshape do to an array?Solution
Step 1: Understand the purpose of reshape
Thereshapefunction changes how the array is organized in terms of rows and columns but does not modify the actual data values.Step 2: Confirm data remains unchanged
The total number of elements stays the same, so no data is lost or changed, only rearranged.Final Answer:
Changes the shape of the array without changing its data -> Option DQuick Check:
reshape changes shape, not data [OK]
- Thinking reshape changes data values
- Assuming reshape deletes elements
- Confusing reshape with sorting
A into a 2x3 matrix in MATLAB?Solution
Step 1: Recall reshape syntax
The correct syntax isreshape(array, rows, columns). So the array comes first, then the new dimensions.Step 2: Check dimensions for 2x3
To reshape into 2 rows and 3 columns, usereshape(A, 2, 3). Options with swapped dimensions or wrong order are incorrect.Final Answer:
reshape(A, 2, 3) -> Option AQuick Check:
reshape(array, rows, cols) [OK]
- Putting dimensions before the array
- Swapping rows and columns
- Using brackets incorrectly
A = 1:6;
B = reshape(A, 3, 2);
disp(B);
Solution
Step 1: Understand reshape fills column-wise
MATLAB fills the reshaped matrix column by column from the original vector. The vector A = [1 2 3 4 5 6].Step 2: Fill 3 rows and 2 columns
Fill first column: 1, 2, 3; second column: 4, 5, 6. So matrix B is:
[1 4;
2 5;
3 6]Final Answer:
[1 4; 2 5; 3 6] -> Option AQuick Check:
reshape fills column-wise [OK]
- Assuming row-wise filling
- Mixing up rows and columns
- Confusing output format
A = 1:8;
B = reshape(A, 3, 3);
Solution
Step 1: Count elements in A
Vector A has 8 elements (1 to 8).Step 2: Check reshape target size
reshape tries to make a 3x3 matrix which needs 9 elements, but A has only 8.Final Answer:
reshape dimensions do not match number of elements -> Option BQuick Check:
Elements must match reshape size [OK]
- Ignoring element count mismatch
- Assuming reshape pads missing elements
- Thinking reshape auto adjusts size
A of size 2x3x4, which MATLAB command correctly reshapes it into a 6x4 matrix without changing the order of elements?Solution
Step 1: Calculate total elements
Original size is 2x3x4 = 24 elements total.Step 2: Confirm reshape size matches total elements
6x4 = 24 elements, so reshape to [6 4] is valid.Step 3: Use correct syntax for reshape with vector size
reshape accepts size as a vector, soreshape(A, [6 4])is correct and clearer for multi-dimensional arrays.Final Answer:
reshape(A, [6 4]) -> Option CQuick Check:
Use vector size for multi-dim reshape [OK]
- Swapping rows and columns
- Using wrong syntax without brackets
- Ignoring total element count
Ready to prove it?
Pass the test and this pattern moves unknown
