📖 Scenario: You are working with data collected from two different sensors. Each sensor gives you a small matrix of numbers. You want to combine these matrices to analyze all the data together.
🎯 Goal: Build a MATLAB program that creates two matrices, sets a configuration variable for concatenation direction, concatenates the matrices accordingly, and then displays the combined matrix.
📋 What You'll Learn
Create two matrices named matrixA and matrixB with exact values
Create a variable concatDirection to specify concatenation direction
Concatenate matrixA and matrixB horizontally or vertically based on concatDirection
Display the resulting concatenated matrix
💡 Why This Matters
🌍 Real World
Combining data from multiple sensors or experiments often requires joining matrices horizontally or vertically for analysis.
💼 Career
Matrix concatenation is a fundamental skill in data processing, scientific computing, and engineering tasks.
Progress0 / 4 steps
1
Create two matrices
Create a matrix called matrixA with values [1 2; 3 4] and a matrix called matrixB with values [5 6; 7 8].
MATLAB
Hint
Use square brackets to create matrices. Separate columns with spaces and rows with semicolons.
2
Set concatenation direction
Create a variable called concatDirection and set it to the string 'horizontal'.
MATLAB
Hint
Use single quotes to create a string in MATLAB.
3
Concatenate matrices based on direction
Use an if statement to check if concatDirection is 'horizontal'. If yes, concatenate matrixA and matrixB horizontally and store in resultMatrix. Otherwise, concatenate them vertically.
MATLAB
Hint
Use strcmp to compare strings in MATLAB. Use square brackets with space for horizontal and semicolon for vertical concatenation.
4
Display the concatenated matrix
Use disp to display the variable resultMatrix.
MATLAB
Hint
Use disp(resultMatrix) to show the matrix in the command window.
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
Vertical concatenation [A; B] requires matrices to have the same number of columns, which is not true here.
Final Answer:
Dimension mismatch error during vertical concatenation -> Option A
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
Step 1: Check dimensions of A and B
A is 2x2, B is 2x3. Both have 2 rows.
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.
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.
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.
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.
Final Answer:
C = [A, B(:,1:2)]; -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.