For loops in MATLAB - Time & Space Complexity
When we use for loops in MATLAB, the program repeats some actions many times. Understanding how this repetition affects the time it takes to run helps us write better code.
We want to know: how does the running time grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
for i = 1:n
disp(i);
end
This code prints numbers from 1 to n, repeating the display action n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
disp(i)command inside the for loop. - How many times: It runs exactly n times, once for each number from 1 to n.
As n gets bigger, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: If you double n, the number of operations also doubles. The growth is steady and direct.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the size of n.
[X] Wrong: "The loop runs faster because it just prints numbers."
[OK] Correct: Even simple actions inside the loop add up. The total time depends on how many times the loop runs, not just what it does each time.
Knowing how loops affect running time helps you explain your code clearly and shows you understand how programs work as they grow.
"What if we added a second nested for loop inside the first? How would the time complexity change?"
Practice
for loop do in MATLAB?Solution
Step 1: Understand the purpose of a for loop
A for loop is designed to repeat commands multiple times based on a range or list.Step 2: Compare options with the definition
Only Repeats a set of commands a fixed number of times correctly describes repeating commands a fixed number of times.Final Answer:
Repeats a set of commands a fixed number of times -> Option DQuick Check:
For loop = repeat commands [OK]
- Thinking for loops run only once
- Confusing for loops with if statements
- Believing for loops stop the program
Solution
Step 1: Identify MATLAB for loop syntax
MATLAB uses 'for variable = start:end' and ends with 'end'.Step 2: Check each option
for i = 1:5 disp(i) end matches MATLAB syntax. Options B, C, and D use other languages' syntax.Final Answer:
for i = 1:5\n disp(i)\nend -> Option AQuick Check:
MATLAB for loop = for i = 1:5 ... end [OK]
- Using parentheses like C or JavaScript
- Using Python or other language syntax
- Forgetting the 'end' keyword
sum = 0; for i = 1:3 sum = sum + i; end disp(sum)
Solution
Step 1: Trace the loop iterations
The loop runs i = 1, 2, 3. sum starts at 0.Step 2: Calculate sum after each iteration
sum = 0+1=1, then 1+2=3, then 3+3=6.Final Answer:
6 -> Option AQuick Check:
1+2+3 = 6 [OK]
- Forgetting to initialize sum
- Adding outside the loop
- Confusing loop range
for i = 1:4 disp(i) i = i + 1; end
Solution
Step 1: Review loop variable usage
The loop variable 'i' is controlled by MATLAB and should not be changed inside the loop.Step 2: Identify the problem
Changing 'i' inside the loop causes unexpected behavior or errors.Final Answer:
Modifying loop variable inside the loop -> Option BQuick Check:
Do not change loop variable inside for loop [OK]
- Changing loop variable inside loop body
- Forgetting 'end' keyword (not the case here)
- Misunderstanding disp function
v containing squares of numbers from 1 to 5?Solution
Step 1: Initialize vector to store results
Pre-allocate vector v with zeros to hold 5 elements.Step 2: Use for loop to assign squares
For each i from 1 to 5, assign v(i) = i squared.Final Answer:
v = zeros(1,5);\nfor i = 1:5\n v(i) = i^2;\nend -> Option CQuick Check:
Pre-allocate vector and assign squares in loop [OK]
- Not pre-allocating vector before loop
- Adding instead of assigning inside loop
- Using wrong operation for squares
Ready to prove it?
Pass the test and this pattern moves unknown
