0
0
MATLABdata~10 mins

Reshaping arrays in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reshaping arrays
Start with original array
Specify new shape dimensions
Check total elements match?
NoError: Cannot reshape
Yes
Rearrange elements into new shape
Output reshaped array
This flow shows how MATLAB reshapes an array by checking if the total number of elements stays the same, then rearranging elements into the new shape.
Execution Sample
MATLAB
A = [1 2 3 4 5 6];
B = reshape(A, 2, 3);
disp(B);
This code reshapes a 1x6 array into a 2x3 matrix and displays it.
Execution Table
StepActionArray ShapeTotal ElementsResult / Output
1Create A1x66[1 2 3 4 5 6]
2Specify new shape (2,3)---
3Check total elements match (6 == 2*3)1x6 and 2x36Match, proceed
4Reshape A to 2x32x36[1 2 3; 4 5 6] (column-wise filling)
5Display B2x36 1 3 5 2 4 6
💡 Execution stops after displaying reshaped array B.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
Aundefined[1 2 3 4 5 6][1 2 3 4 5 6][1 2 3 4 5 6]
Bundefinedundefined[1 2 3; 4 5 6][1 2 3; 4 5 6]
Key Moments - 3 Insights
Why does MATLAB fill the reshaped array column-wise, not row-wise?
MATLAB stores arrays in column-major order, so reshape fills elements down columns first, as shown in step 4 of the execution_table.
What happens if the total elements don't match when reshaping?
MATLAB throws an error because the reshape function requires the total number of elements to stay the same, as checked in step 3.
Does reshape change the original array A?
No, reshape creates a new array B with the new shape; A remains unchanged, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of B(2,1)?
A3
B4
C2
D5
💡 Hint
Check the reshaped array values in step 4 of execution_table.
At which step does MATLAB verify if the reshape is possible?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where total elements are compared in execution_table.
If A had 8 elements instead of 6, what would happen at step 3?
AError because total elements don't match
BReshape proceeds normally
CArray B becomes larger
DOriginal array A changes shape
💡 Hint
Refer to the explanation in key_moments about element count matching.
Concept Snapshot
reshape(array, rows, cols)
- Changes array shape without changing data order
- Total elements must match (rows*cols = numel(array))
- MATLAB fills reshaped array column-wise
- Original array stays unchanged
- Errors if sizes mismatch
Full Transcript
This visual trace shows how MATLAB reshapes arrays. First, the original array A is created with 6 elements. Then, the new shape 2 rows by 3 columns is specified. MATLAB checks if the total elements match (6 == 2*3). Since they match, it rearranges elements column-wise into the new 2x3 array B. The original array A remains unchanged. The reshaped array B is displayed with elements filled down columns. If the total elements did not match, MATLAB would give an error. This process helps change how data is organized without changing the data itself.