Bird
Raised Fist0
MATLABdata~20 mins

Nested loops in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with matrix assignment
What is the output of the following MATLAB code?
result = zeros(2,3);
for i = 1:2
    for j = 1:3
        result(i,j) = i + j;
    end
end
result
MATLAB
result = zeros(2,3);
for i = 1:2
    for j = 1:3
        result(i,j) = i + j;
    end
end
result
A[2 3 4; 3 4 5]
B[1 2 3; 2 3 4]
C[3 4 5; 4 5 6]
D[1 1 1; 1 1 1]
Attempts:
2 left
💡 Hint
Remember that MATLAB indices start at 1 and the loops assign i + j to each element.
❓ Predict Output
intermediate
2:00remaining
Nested loops with conditional break
What is the value of variable count after running this MATLAB code?
count = 0;
for i = 1:5
    for j = 1:5
        count = count + 1;
        if i * j > 6
            break;
        end
    end
end
count
MATLAB
count = 0;
for i = 1:5
    for j = 1:5
        count = count + 1;
        if i * j > 6
            break;
        end
    end
end
count
A20
B25
C16
D10
Attempts:
2 left
💡 Hint
The inner loop breaks when i*j > 6, so not all inner iterations run fully. Note that count increments before the check.
🔧 Debug
advanced
2:00remaining
Identify the error in nested loops
What error does this MATLAB code produce when run?
for i = 1:3
    for j = 1:3
        disp(i + j
    end
end
MATLAB
for i = 1:3
    for j = 1:3
        disp(i + j
    end
end
ASyntaxError: Missing closing parenthesis
BRuntimeError: Variable j undefined
CIndexError: Index exceeds matrix dimensions
DNo error, outputs sums
Attempts:
2 left
💡 Hint
Check the parentheses in the disp function call.
🧠 Conceptual
advanced
2:00remaining
Number of iterations in nested loops
How many times will the innermost statement run in this MATLAB code?
count = 0;
for i = 1:4
    for j = i:4
        count = count + 1;
    end
end
count
MATLAB
count = 0;
for i = 1:4
    for j = i:4
        count = count + 1;
    end
end
count
A16
B10
C8
D6
Attempts:
2 left
💡 Hint
The inner loop starts at i and goes to 4, so the number of iterations decreases as i increases.
🚀 Application
expert
3:00remaining
Create a multiplication table matrix
Which MATLAB code correctly creates a 5x5 multiplication table matrix M where M(i,j) = i*j using nested loops?
A
M = zeros(5,5);
for i = 1:5
    for j = 1:5
        M(i,j) = i + j;
    end
end
B
M = zeros(5,5);
for i = 1:5
    for j = 1:5
        M(j,i) = i * j;
    end
end
C
M = zeros(5,5);
for i = 1:5
    for j = 1:5
        M(i+j) = i * j;
    end
end
D
M = zeros(5);
for i = 1:5
    for j = 1:5
        M(i,j) = i * j;
    end
end
Attempts:
2 left
💡 Hint
Remember MATLAB matrices are indexed by row and column, and multiplication table means product of indices.

Practice

(1/5)
1. What does a nested loop in MATLAB do?
for i = 1:2
for j = 1:3
disp([i, j])
end
end
easy
A. Runs one loop inside another to repeat tasks multiple times
B. Runs two loops one after another without overlap
C. Creates a single loop with combined ranges
D. Stops execution after the first loop finishes

Solution

  1. Step 1: Understand the loop structure

    The outer loop runs with i from 1 to 2, and inside it, the inner loop runs with j from 1 to 3.
  2. Step 2: Identify nested loop behavior

    For each i, the inner loop runs fully for all j values, repeating tasks multiple times.
  3. Final Answer:

    Runs one loop inside another to repeat tasks multiple times -> Option A
  4. Quick Check:

    Nested loops repeat inner loop for each outer loop iteration [OK]
Hint: Nested loops repeat inner loop fully for each outer loop step [OK]
Common Mistakes:
  • Thinking loops run sequentially without nesting
  • Confusing nested loops with single combined loops
  • Assuming loops stop early without completing inner loop
2. Which of the following is the correct syntax for a nested for loop in MATLAB?
easy
A. for i = 1:3 disp(i) for j = 1:2 disp(j) end
B. for i = 1:3 for j = 1:2 disp(i+j) end
C. for i = 1:3 for j = 1:2 disp(i+j) end
D. for i = 1:3 for j = 1:2 disp(i+j) end end

Solution

  1. Step 1: Check proper loop nesting and end statements

    for i = 1:3 for j = 1:2 disp(i+j) end end correctly nests the inner loop inside the outer loop and closes both with end.
  2. Step 2: Identify syntax errors in other options

    The other options miss proper indentation or have missing end statements causing syntax errors.
  3. Final Answer:

    for i = 1:3 for j = 1:2 disp(i+j) end end -> Option D
  4. Quick Check:

    Proper nested loops need matching end for each for [OK]
Hint: Each for loop must have its own end statement [OK]
Common Mistakes:
  • Missing end for inner or outer loop
  • Incorrect indentation causing confusion
  • Starting loops without proper nesting
3. What is the output of this MATLAB code?
for i = 1:2
  for j = 1:2
    fprintf('%d ', i*j)
  end
  fprintf('\n')
end
medium
A. 1 2 2 4
B. 1 1 2 2
C. 1 2 2 4
D. 2 4 1 2

Solution

  1. Step 1: Calculate inner loop outputs for each outer loop value

    When i=1, j=1:2, outputs are 1*1=1 and 1*2=2. When i=2, j=1:2, outputs are 2*1=2 and 2*2=4.
  2. Step 2: Format output with new lines

    Each inner loop prints values with space, then outer loop prints newline. So output lines are '1 2 ' and '2 4 '.
  3. Final Answer:

    1 2 2 4 -> Option C
  4. Quick Check:

    Multiply i and j in nested loops for output [OK]
Hint: Multiply outer and inner loop vars for each output [OK]
Common Mistakes:
  • Ignoring newline after inner loop
  • Mixing up i and j values
  • Printing all values in one line
4. Find the error in this nested loop code:
for i = 1:3
  for j = 1:2
    disp(i+j)
end
medium
A. Missing end for outer loop
B. Inner loop range is incorrect
C. disp function is used incorrectly
D. Loop variables i and j are not initialized

Solution

  1. Step 1: Check loop endings

    The inner loop has an end, but the outer loop does not have its closing end.
  2. Step 2: Identify syntax error cause

    Missing the outer loop's end causes MATLAB to throw an error.
  3. Final Answer:

    Missing end for outer loop -> Option A
  4. Quick Check:

    Each for loop needs its own end [OK]
Hint: Count ends: one per for loop [OK]
Common Mistakes:
  • Forgetting to close outer loop
  • Assuming one end closes both loops
  • Misreading error messages
5. You want to create a 3x3 multiplication table using nested loops in MATLAB. Which code correctly fills matrix M with products of row and column indices?
hard
A. M = zeros(3); for i = 1:3 for j = 1:3 M(j,i) = i+j; end end
B. M = zeros(3); for i = 1:3 for j = 1:3 M(i,j) = i*j; end end
C. M = zeros(3); for i = 1:3 for j = 1:3 M(i,j) = i+j; end end
D. M = zeros(3); for i = 1:3 for j = 1:3 M(i,j) = i-j; end end

Solution

  1. Step 1: Understand the multiplication table goal

    The table should store products of row and column indices, so M(i,j) = i*j.
  2. Step 2: Check each option's assignment

    M = zeros(3); for i = 1:3 for j = 1:3 M(i,j) = i*j; end end correctly assigns M(i,j) = i*j. The other options assign sums or differences, not products.
  3. Final Answer:

    M = zeros(3); for i = 1:3 for j = 1:3 M(i,j) = i*j; end end -> Option B
  4. Quick Check:

    Multiplication table uses product of indices [OK]
Hint: Assign M(i,j) = i*j for multiplication table [OK]
Common Mistakes:
  • Using addition or subtraction instead of multiplication
  • Swapping indices in assignment
  • Not initializing matrix before loops
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