Comparison operators in MATLAB - Time & Space Complexity
We want to understand how long it takes to compare values in MATLAB as the input size grows.
How does the time needed change when we compare many elements?
Analyze the time complexity of the following code snippet.
arr = randi(100, 1, n);
count = 0;
for i = 1:n
if arr(i) > 50
count = count + 1;
end
end
This code counts how many numbers in the array are greater than 50.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each element to 50 using the > operator inside a loop.
- How many times: Exactly once for each element in the array, so n times.
As the array gets bigger, the number of comparisons grows directly with the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: Doubling the input doubles the number of comparisons.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of elements.
[X] Wrong: "Comparison operators take constant time no matter how many elements we have."
[OK] Correct: While one comparison is quick, doing many comparisons one after another adds up, so total time grows with the number of elements.
Understanding how simple comparisons add up helps you explain how your code handles bigger data smoothly and efficiently.
"What if we compared pairs of elements inside two nested loops? How would the time complexity change?"
Practice
5 > 3 return?Solution
Step 1: Understand the comparison operator
The operator > checks if the left value is greater than the right value.Step 2: Compare the values 5 and 3
Since 5 is greater than 3, the expression returns true, which is represented as 1 in MATLAB.Final Answer:
1 (true) -> Option BQuick Check:
5 > 3 = 1 [OK]
- Confusing true (1) with false (0)
- Thinking it returns the bigger number
- Using wrong operator like < instead of >
a is equal to 10?Solution
Step 1: Identify the equality operator in MATLAB
MATLAB uses double equal signs==to test equality, not a single equal sign.Step 2: Check each option
a = 10 assigns 10 to a, not compare. a == 10 correctly compares a to 10. a === 10 uses triple equals which is invalid in MATLAB. a != 10 uses not equal operator incorrectly.Final Answer:
a == 10 -> Option DQuick Check:
Equality test uses == [OK]
- Using single = instead of ==
- Using === which is not MATLAB syntax
- Confusing != with ~= for not equal
a = [1 3 5]; b = [2 3 4]; result = a <= b;
Solution
Step 1: Understand element-wise comparison
The operator <= compares each element ofawith the corresponding element ofb.Step 2: Compare each pair
1 <= 2 is true (1), 3 <= 3 is true (1), 5 <= 4 is false (0).Final Answer:
[1 1 0] -> Option AQuick Check:
Element-wise <= comparison = [1 1 0] [OK]
- Thinking it returns a single value
- Mixing up < and <= operators
- Forgetting element-wise comparison
if a = 5
disp('Equal');
endSolution
Step 1: Check the if condition syntax
In MATLAB, the if condition requires a logical expression, not an assignment.Step 2: Identify the operator mistake
The code uses=which assigns 5 toa, instead of==which comparesato 5.Final Answer:
Using = instead of == in if condition -> Option CQuick Check:
Comparison uses ==, not = in conditions [OK]
- Using = instead of ==
- Adding unnecessary parentheses
- Misusing disp function
A = [4 7 2 9] and B = [5 7 3 8], write a MATLAB expression using comparison operators to create a logical array that is true where elements of A are greater than or equal to B and false otherwise. What is the resulting array?Solution
Step 1: Use element-wise comparison operator
Use the operator >= to compare each element ofAwithBelement-wise.Step 2: Compare each element pair
4 >= 5 is false (0), 7 >= 7 is true (1), 2 >= 3 is false (0), 9 >= 8 is true (1).Final Answer:
[0 1 0 1] -> Option AQuick Check:
A >= B = [0 1 0 1] [OK]
- Using > instead of >=
- Confusing logical array with numeric values
- Mixing element-wise with matrix operations
Ready to prove it?
Pass the test and this pattern moves unknown
