Bird
Raised Fist0
MATLABdata~20 mins

Matrix creation in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this matrix creation code?
Consider the following MATLAB code that creates a matrix. What will be the resulting matrix?
MATLAB
A = zeros(2,3) + eye(2,3); disp(A);
A[0 0 0; 0 0 0]
B[1 0; 0 1; 0 0]
C[1 0 0; 0 1 0]
D[1 1 1; 1 1 1]
Attempts:
2 left
💡 Hint
Remember that eye(m,n) creates an m-by-n matrix with ones on the main diagonal and zeros elsewhere.
❓ Predict Output
intermediate
1:30remaining
What is the size of the matrix created?
What is the size of the matrix B created by this MATLAB code?
MATLAB
B = ones(4) * 5;
A4x4
B5x5
C4x1
D1x4
Attempts:
2 left
💡 Hint
ones(n) creates an n-by-n matrix of ones.
❓ Predict Output
advanced
2:00remaining
What does this matrix creation code output?
What is the output of this MATLAB code snippet?
MATLAB
C = diag([3 4 5]); disp(C);
A[3 0 0; 4 0 0; 5 0 0]
B[3 4 5; 0 0 0; 0 0 0]
C[0 3 0; 0 4 0; 0 0 5]
D[3 0 0; 0 4 0; 0 0 5]
Attempts:
2 left
💡 Hint
The diag function creates a diagonal matrix from the input vector.
❓ Predict Output
advanced
2:30remaining
What is the output of this matrix concatenation?
What matrix does this MATLAB code produce?
MATLAB
D = [1 2; 3 4]; E = [5 6; 7 8]; F = [D E; E D]; disp(F);
A[1 2 5 6; 3 4 7 8; 5 6 1 2; 7 8 3 4]
B[1 2 3 4; 5 6 7 8; 1 2 3 4; 5 6 7 8]
C[1 2 5 6; 3 4 7 8; 1 2 5 6; 3 4 7 8]
D[5 6 1 2; 7 8 3 4; 1 2 5 6; 3 4 7 8]
Attempts:
2 left
💡 Hint
Square brackets concatenate matrices horizontally and vertically.
❓ Predict Output
expert
3:00remaining
What is the output of this complex matrix creation code?
What is the output of this MATLAB code snippet?
MATLAB
G = repmat([1 2; 3 4], 2, 3); disp(G);
A[1 2 1 2; 3 4 3 4; 1 2 1 2; 3 4 3 4]
B[1 2 1 2 1 2; 3 4 3 4 3 4; 1 2 1 2 1 2; 3 4 3 4 3 4]
C[1 2 3 4 1 2; 3 4 1 2 3 4; 1 2 3 4 1 2; 3 4 1 2 3 4]
D[1 2 3 4 5 6; 7 8 9 10 11 12; 1 2 3 4 5 6; 7 8 9 10 11 12]
Attempts:
2 left
💡 Hint
repmat repeats the matrix the specified number of times in rows and columns.

Practice

(1/5)
1. Which of the following correctly creates a 2x3 matrix in MATLAB?
easy
A. [1 2; 3 4 5]
B. [1, 2; 3, 4; 5, 6]
C. [1 2 3; 4 5 6]
D. [1; 2; 3; 4]

Solution

  1. Step 1: Understand matrix dimensions

    A 2x3 matrix has 2 rows and 3 columns.
  2. Step 2: Check each option's shape

    [1 2 3; 4 5 6] has 2 rows separated by semicolon and 3 columns separated by spaces, so it is 2x3.
  3. Final Answer:

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

    2 rows and 3 columns = [1 2 3; 4 5 6] [OK]
Hint: Rows separated by semicolons, columns by spaces or commas [OK]
Common Mistakes:
  • Using commas instead of semicolons for rows
  • Mismatched number of columns in rows
  • Confusing rows and columns
2. Which MATLAB code correctly creates a 3x3 identity matrix?
easy
A. eye(3)
B. zeros(3,3)
C. ones(3)
D. diag(3)

Solution

  1. Step 1: Recall identity matrix function

    In MATLAB, eye(n) creates an n-by-n identity matrix.
  2. Step 2: Check options

    eye(3) uses eye(3), which is the correct function for a 3x3 identity matrix.
  3. Final Answer:

    eye(3) -> Option A
  4. Quick Check:

    Identity matrix = eye(3) [OK]
Hint: Use eye(n) for identity matrix of size n [OK]
Common Mistakes:
  • Using zeros or ones instead of eye
  • Using diag with incorrect vector size
  • Confusing identity with zero matrix
3. What is the output of the following MATLAB code?
A = [1 2; 3 4];
B = zeros(2,3);
C = [A B];
disp(C)
medium
A. [1 2; 3 4; 0 0 0]
B. [1 2 0 0 0; 3 4 0 0 0]
C. [1 2; 3 4; 0 0]
D. Error: Matrix dimensions must agree.

Solution

  1. Step 1: Understand matrix sizes

    A is 2x2, B is 2x3 of zeros.
  2. Step 2: Concatenate horizontally

    [A B] joins matrices side by side, resulting in 2 rows and 5 columns.
  3. Final Answer:

    [1 2 0 0 0; 3 4 0 0 0] -> Option B
  4. Quick Check:

    Horizontal concat of 2x2 and 2x3 = 2x5 matrix [OK]
Hint: Horizontal concat needs same row count [OK]
Common Mistakes:
  • Trying to concat matrices with different row counts vertically
  • Confusing zeros size
  • Expecting error instead of valid concat
4. Identify the error in this MATLAB code:
M = [1 2 3; 4 5];
medium
A. Missing semicolon between rows
B. Matrix elements must be separated by commas
C. Use parentheses instead of square brackets
D. Rows have different number of columns

Solution

  1. Step 1: Check matrix row sizes

    First row has 3 elements, second row has 2 elements.
  2. Step 2: Understand MATLAB matrix rules

    All rows must have the same number of columns; here they differ.
  3. Final Answer:

    Rows have different number of columns -> Option D
  4. Quick Check:

    Matrix rows must match column count [OK]
Hint: All rows must have equal columns in matrix [OK]
Common Mistakes:
  • Thinking commas are required between rows
  • Using parentheses instead of brackets
  • Ignoring unequal row lengths
5. You want to create a 4x4 matrix with 1s on the diagonal and 0.5 everywhere else. Which MATLAB code achieves this?
hard
A. M = 0.5*ones(4); M(eye(4) == 1) = 1;
B. M = eye(4) + 0.5;
C. M = 0.5*ones(4) + eye(4);
D. M = ones(4); M(eye(4) == 1) = 0.5;

Solution

  1. Step 1: Create a 4x4 matrix of 0.5

    Using 0.5*ones(4) sets all elements to 0.5.
  2. Step 2: Replace diagonal elements with 1

    Using logical indexing M(eye(4) == 1) = 1; sets diagonal to 1.
  3. Final Answer:

    M = 0.5*ones(4); M(eye(4) == 1) = 1; -> Option A
  4. Quick Check:

    Diagonal 1, others 0.5 = M = 0.5*ones(4); M(eye(4) == 1) = 1; [OK]
Hint: Set all to 0.5 then replace diagonal with 1 [OK]
Common Mistakes:
  • Adding eye(4) instead of replacing diagonal
  • Mixing addition and multiplication incorrectly
  • Setting diagonal to 0.5 instead of 1
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