Bird
Raised Fist0
MATLABdata~10 mins

Reshaping arrays in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Reshaping arrays
Start with original array
↓
Specify new shape dimensions
↓
Check total elements match?
No→Error: 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.

Practice

(1/5)
1. What does the MATLAB function reshape do to an array?
easy
A. Sorts the array elements in ascending order
B. Changes the data values but keeps the shape the same
C. Deletes elements from the array
D. Changes the shape of the array without changing its data

Solution

  1. Step 1: Understand the purpose of reshape

    The reshape function changes how the array is organized in terms of rows and columns but does not modify the actual data values.
  2. Step 2: Confirm data remains unchanged

    The total number of elements stays the same, so no data is lost or changed, only rearranged.
  3. Final Answer:

    Changes the shape of the array without changing its data -> Option D
  4. Quick Check:

    reshape changes shape, not data [OK]
Hint: Remember reshape only rearranges elements, data stays same [OK]
Common Mistakes:
  • Thinking reshape changes data values
  • Assuming reshape deletes elements
  • Confusing reshape with sorting
2. Which of the following is the correct syntax to reshape a 1x6 vector A into a 2x3 matrix in MATLAB?
easy
A. reshape(A, 2, 3)
B. reshape(2, 3, A)
C. reshape(A, [3 2])
D. reshape(A, 3, 2)

Solution

  1. Step 1: Recall reshape syntax

    The correct syntax is reshape(array, rows, columns). So the array comes first, then the new dimensions.
  2. Step 2: Check dimensions for 2x3

    To reshape into 2 rows and 3 columns, use reshape(A, 2, 3). Options with swapped dimensions or wrong order are incorrect.
  3. Final Answer:

    reshape(A, 2, 3) -> Option A
  4. Quick Check:

    reshape(array, rows, cols) [OK]
Hint: Syntax is reshape(array, rows, columns) [OK]
Common Mistakes:
  • Putting dimensions before the array
  • Swapping rows and columns
  • Using brackets incorrectly
3. What is the output of the following MATLAB code?
A = 1:6;
B = reshape(A, 3, 2);
disp(B);
medium
A. [1 4; 2 5; 3 6]
B. [1 2; 3 4; 5 6]
C. [1 3 5; 2 4 6]
D. [1 2 3; 4 5 6]

Solution

  1. 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].
  2. 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]
  3. Final Answer:

    [1 4; 2 5; 3 6] -> Option A
  4. Quick Check:

    reshape fills column-wise [OK]
Hint: Remember MATLAB fills columns first in reshape [OK]
Common Mistakes:
  • Assuming row-wise filling
  • Mixing up rows and columns
  • Confusing output format
4. Identify the error in this MATLAB code:
A = 1:8;
B = reshape(A, 3, 3);
medium
A. Variable A is not defined correctly
B. reshape dimensions do not match number of elements
C. Syntax error in reshape function
D. No error, code runs fine

Solution

  1. Step 1: Count elements in A

    Vector A has 8 elements (1 to 8).
  2. Step 2: Check reshape target size

    reshape tries to make a 3x3 matrix which needs 9 elements, but A has only 8.
  3. Final Answer:

    reshape dimensions do not match number of elements -> Option B
  4. Quick Check:

    Elements must match reshape size [OK]
Hint: Check total elements before reshaping [OK]
Common Mistakes:
  • Ignoring element count mismatch
  • Assuming reshape pads missing elements
  • Thinking reshape auto adjusts size
5. Given a 3D array A of size 2x3x4, which MATLAB command correctly reshapes it into a 6x4 matrix without changing the order of elements?
hard
A. reshape(A, [2 3 4])
B. reshape(A, 4, 6)
C. reshape(A, [6 4])
D. reshape(A, [4 6])

Solution

  1. Step 1: Calculate total elements

    Original size is 2x3x4 = 24 elements total.
  2. Step 2: Confirm reshape size matches total elements

    6x4 = 24 elements, so reshape to [6 4] is valid.
  3. Step 3: Use correct syntax for reshape with vector size

    reshape accepts size as a vector, so reshape(A, [6 4]) is correct and clearer for multi-dimensional arrays.
  4. Final Answer:

    reshape(A, [6 4]) -> Option C
  5. Quick Check:

    Use vector size for multi-dim reshape [OK]
Hint: Use reshape(A, [rows cols]) for multi-dim arrays [OK]
Common Mistakes:
  • Swapping rows and columns
  • Using wrong syntax without brackets
  • Ignoring total element count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard