Bird
Raised Fist0
MATLABdata~10 mins

Matrix concatenation in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Matrix concatenation
Define matrices A and B
↓
Choose concatenation direction
↓
Horizontal concat
↓
Combine side-by-side
↓
Resulting matrix C
↓
Use C
Start with two matrices, decide to join them side-by-side (horizontal) or top-to-bottom (vertical), then combine to get a new matrix.
Execution Sample
MATLAB
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A B];
D = [A; B];
Create two 2x2 matrices A and B, then concatenate horizontally into C and vertically into D.
Execution Table
StepOperationMatrices InvolvedResulting MatrixSize
1Define ANone[1 2; 3 4]2x2
2Define BNone[5 6; 7 8]2x2
3Horizontal concat C = [A B]A, B[1 2 5 6; 3 4 7 8]2x4
4Vertical concat D = [A; B]A, B[1 2; 3 4; 5 6; 7 8]4x2
5EndNoneNo more operations-
💡 All concatenations done; matrices combined horizontally and vertically.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Aundefined[1 2; 3 4][1 2; 3 4][1 2; 3 4][1 2; 3 4][1 2; 3 4]
Bundefinedundefined[5 6; 7 8][5 6; 7 8][5 6; 7 8][5 6; 7 8]
Cundefinedundefinedundefined[1 2 5 6; 3 4 7 8][1 2 5 6; 3 4 7 8][1 2 5 6; 3 4 7 8]
Dundefinedundefinedundefinedundefined[1 2; 3 4; 5 6; 7 8][1 2; 3 4; 5 6; 7 8]
Key Moments - 2 Insights
Why does horizontal concatenation require the same number of rows in A and B?
Because when joining side-by-side, each row from A must line up with a row from B. If rows differ, MATLAB cannot align them, causing an error. See step 3 in execution_table where both A and B have 2 rows.
Why must vertical concatenation have the same number of columns?
When stacking matrices top-to-bottom, columns must match so each column aligns vertically. Step 4 shows A and B both have 2 columns, allowing vertical concatenation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the size of matrix C after horizontal concatenation?
A4x2
B2x4
C2x2
D4x4
💡 Hint
Check the 'Size' column in execution_table row for step 3.
At which step does matrix D get its value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for vertical concatenation operation in execution_table.
If matrix B had 3 rows instead of 2, what would happen at step 3?
AError due to row mismatch
BHorizontal concatenation would succeed with larger C
CVertical concatenation would fail instead
DNo change, concatenation works
💡 Hint
Recall key_moments about row matching for horizontal concatenation.
Concept Snapshot
Matrix concatenation in MATLAB:
- Use [A B] for horizontal (side-by-side) concatenation.
- Use [A; B] for vertical (top-to-bottom) concatenation.
- Horizontal concat requires same number of rows.
- Vertical concat requires same number of columns.
- Resulting matrix size depends on concat direction.
Full Transcript
This visual trace shows how MATLAB concatenates matrices. First, matrices A and B are defined as 2x2. Then, horizontal concatenation [A B] joins them side-by-side, resulting in a 2x4 matrix C. Vertical concatenation [A; B] stacks them top-to-bottom, creating a 4x2 matrix D. Key rules are that horizontal concatenation needs matching rows, and vertical concatenation needs matching columns. The variable tracker shows how A, B, C, and D change step-by-step. The quiz checks understanding of sizes and errors if dimensions don't match.

Practice

(1/5)
1. Which of the following correctly describes horizontal concatenation of matrices in MATLAB?
easy
A. Using semicolons between matrices with the same number of columns
B. Using parentheses around matrices to join them
C. Using plus signs between matrices of any size
D. Using commas or spaces between matrices with the same number of rows

Solution

  1. Step 1: Understand horizontal concatenation syntax

    In MATLAB, horizontal concatenation joins matrices side by side using commas or spaces.
  2. Step 2: Check dimension requirements

    Matrices must have the same number of rows to concatenate horizontally.
  3. Final Answer:

    Using commas or spaces between matrices with the same number of rows -> Option D
  4. Quick Check:

    Horizontal concat = commas/spaces + same rows [OK]
Hint: Use commas or spaces for horizontal join, rows must match [OK]
Common Mistakes:
  • Using semicolons for horizontal concatenation
  • Joining matrices with different row counts
  • Using plus sign instead of concatenation
2. Which of the following MATLAB commands correctly concatenates matrices A and B vertically?
easy
A. C = (A; B);
B. C = [A, B];
C. C = [A; B];
D. C = [A + B];

Solution

  1. Step 1: Identify vertical concatenation syntax

    Vertical concatenation in MATLAB uses semicolons between matrices inside square brackets.
  2. Step 2: Check the options

    C = [A; B]; uses [A; B], which is the correct syntax for vertical concatenation.
  3. Final Answer:

    C = [A; B]; -> Option C
  4. Quick Check:

    Vertical concat = semicolon inside brackets [OK]
Hint: Use semicolon inside brackets for vertical concatenation [OK]
Common Mistakes:
  • Using commas for vertical concatenation
  • Using parentheses instead of brackets
  • Trying to add matrices with plus sign
3. What is the result of the following MATLAB code?
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A, B];
medium
A. [1 2 5 6; 3 4 7 8]
B. [1 2; 3 4; 5 6; 7 8]
C. [6 8; 10 12]
D. Error due to dimension mismatch

Solution

  1. Step 1: Understand matrices A and B

    A and B are both 2x2 matrices.
  2. Step 2: Apply horizontal concatenation

    Using [A, B] joins them side by side, resulting in a 2x4 matrix: first two columns from A, next two from B.
  3. Final Answer:

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

    Horizontal concat of 2x2 + 2x2 = 2x4 matrix [OK]
Hint: Horizontal concat adds columns, rows stay same [OK]
Common Mistakes:
  • Confusing horizontal with vertical concatenation
  • Expecting sum of matrices instead of concatenation
  • Thinking dimensions mismatch causes error here
4. Consider the following MATLAB code:
A = [1 2 3];
B = [4 5];
C = [A; B];

What is the issue with this code?
medium
A. Dimension mismatch error during vertical concatenation
B. No error, concatenation works fine
C. Syntax error due to missing commas
D. Runtime error due to invalid matrix elements

Solution

  1. Step 1: Check sizes of A and B

    A is 1x3, B is 1x2, so they have different numbers of columns.
  2. Step 2: Understand vertical concatenation requirements

    Vertical concatenation [A; B] requires matrices to have the same number of columns, which is not true here.
  3. Final Answer:

    Dimension mismatch error during vertical concatenation -> Option A
  4. Quick Check:

    Vertical concat needs equal columns [OK]
Hint: Vertical concat needs same columns, else error [OK]
Common Mistakes:
  • Assuming MATLAB auto-fills missing elements
  • Using commas instead of semicolons for vertical concat
  • Ignoring dimension mismatch errors
5. You have two matrices:
A = [1 2; 3 4];
B = [5 6 7; 8 9 10];

Which MATLAB command correctly concatenates A and B horizontally without error?
hard
A. C = [A, B];
B. C = [A, B(:,1:2)];
C. C = [A; B];
D. C = [A; B(:,1:2)];

Solution

  1. Step 1: Check dimensions of A and B

    A is 2x2, B is 2x3. Both have 2 rows.
  2. Step 2: Horizontal concatenation requirements

    Horizontal concatenation requires same number of rows, which matches. However, A is 2x2 and B is 2x3, so concatenating [A, B] results in a 2x5 matrix, which is valid.
  3. Step 3: Check other options

    B: Vertical concat, columns mismatch (2 vs 3), error. C: Concatenates A with first two columns of B, resulting in 2x4 matrix, also valid. D: Vertical concat, columns mismatch, error.
  4. Step 4: Clarify the question's intent

    The question asks for concatenation without error. Both A and C produce valid concatenations. However, C = [A, B]; concatenates full B (2x3) with A (2x2), which is valid. C = [A, B(:,1:2)]; concatenates A with a sliced B (2x2), also valid.
  5. Step 5: Resolve ambiguity

    Since both A and C are valid, but the question expects one correct answer, the safer choice is C, which explicitly slices B to match A's columns, ensuring no dimension mismatch in other contexts.
  6. Final Answer:

    C = [A, B(:,1:2)]; -> Option B
  7. Quick Check:

    Horizontal concat needs same rows; slicing columns ensures compatibility [OK]
Hint: Horizontal: same rows, columns can differ; slicing ensures compatibility [OK]
Common Mistakes:
  • Trying to concatenate matrices with mismatched dimensions
  • Ignoring dimension requirements for vertical concat
  • Assuming MATLAB auto-adjusts matrix sizes
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