Challenge - 5 Problems
Matrix Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of horizontal concatenation?
Consider the following MATLAB code:
What will be displayed?
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A B];
disp(C);
What will be displayed?
MATLAB
A = [1 2; 3 4]; B = [5 6; 7 8]; C = [A B]; disp(C);
Attempts:
2 left
💡 Hint
Horizontal concatenation joins matrices side by side, so columns add up.
✗ Incorrect
The code concatenates matrices A and B horizontally, so the columns of B are appended to the columns of A, resulting in a 2x4 matrix.
❓ Predict Output
intermediate2:00remaining
What is the output of vertical concatenation?
Given the MATLAB code:
What will be displayed?
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A; B];
disp(C);
What will be displayed?
MATLAB
A = [1 2; 3 4]; B = [5 6; 7 8]; C = [A; B]; disp(C);
Attempts:
2 left
💡 Hint
Vertical concatenation stacks matrices on top of each other, so rows add up.
✗ Incorrect
The code concatenates matrices A and B vertically, so the rows of B are appended below the rows of A, resulting in a 4x2 matrix.
❓ Predict Output
advanced2:00remaining
What error occurs with mismatched dimensions in horizontal concatenation?
What happens when you run this MATLAB code?
A = [1 2; 3 4];
B = [5 6 7];
C = [A B];
MATLAB
A = [1 2; 3 4]; B = [5 6 7]; C = [A B];
Attempts:
2 left
💡 Hint
Horizontal concatenation requires the same number of rows.
✗ Incorrect
Matrix A has 2 rows, but B has 1 row. MATLAB cannot concatenate horizontally matrices with different row counts, so it throws a dimension mismatch error.
❓ Predict Output
advanced2:00remaining
What is the output of nested concatenation?
What does this MATLAB code output?
A = [1 2];
B = [3 4];
C = [5 6];
D = [A; B C];
disp(D);
MATLAB
A = [1 2]; B = [3 4]; C = [5 6]; D = [A; B C]; disp(D);
Attempts:
2 left
💡 Hint
Check the sizes of B and C before concatenation.
✗ Incorrect
B is 1x2 and C is 1x2, so [B C] is 1x4. A is 1x2. Vertical concatenation [A; B C] requires same number of columns, but 2 != 4, so MATLAB throws an error.
🧠 Conceptual
expert2:00remaining
How many elements are in the resulting matrix after concatenation?
Given these matrices:
How many elements does matrix C contain?
A = ones(3,4);
B = zeros(3,2);
C = [A B];
How many elements does matrix C contain?
MATLAB
A = ones(3,4); B = zeros(3,2); C = [A B];
Attempts:
2 left
💡 Hint
Count rows and columns after concatenation, then multiply.
✗ Incorrect
A is 3x4, B is 3x2. Horizontal concatenation results in 3x(4+2) = 3x6 matrix. Number of elements = 3*6 = 18.