0
0
MATLABdata~10 mins

For loops in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a for loop that counts from 1 to 5.

MATLAB
for i = [1]
    disp(i)
end
Drag options to blanks, or click blank then click option'
A1 to 5
B1,5
C1:5
D[1 5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas instead of colon to create the range.
Using square brackets which create a vector but not a range for the loop.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a for loop.

MATLAB
total = 0;
for n = 1:10
    total = total + [1];
end
Drag options to blanks, or click blank then click option'
A10
Btotal
C1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the total to itself instead of the loop variable.
Adding a fixed number like 10 or 1 instead of the loop variable.
3fill in blank
hard

Fix the error in the for loop to display numbers from 5 down to 1.

MATLAB
for i = [1]
    disp(i)
end
Drag options to blanks, or click blank then click option'
A1:5
B5:-1:1
C5:1
D1:-1:5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5:1 which counts up and results in an empty loop.
Using 1:-1:5 which is an invalid range.
4fill in blank
hard

Fill both blanks to create a for loop that prints only even numbers from 2 to 10.

MATLAB
for i = [1]:[2]:10
    disp(i)
end
Drag options to blanks, or click blank then click option'
A2
B1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the start or step which prints odd numbers.
Using 3 as the step which skips some even numbers.
5fill in blank
hard

Fill all three blanks to create a for loop that prints the squares of numbers from 1 to 5.

MATLAB
for [1] = [2]:[3]:5
    disp([1]^2)
end
Drag options to blanks, or click blank then click option'
Ai
B1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a step other than 1 which skips numbers.
Using a variable name other than i which is conventional here.