Logical values in MATLAB - Time & Space Complexity
Let's see how using logical values affects the time it takes for a program to run.
We want to know how the program's steps grow when working with true or false values.
Analyze the time complexity of the following code snippet.
n = 1000;
A = rand(n,1);
B = A > 0.5 & A;
countTrue = sum(B);
This code creates a list of numbers, checks which are greater than 0.5, combines logical checks, and counts how many are true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Logical AND operation on each element of the array.
- How many times: Once for each of the n elements in the array.
Each element is checked once, so the work grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 logical checks |
| 100 | About 100 logical checks |
| 1000 | About 1000 logical checks |
Pattern observation: When the input size doubles, the number of operations also doubles.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of elements you check.
[X] Wrong: "Logical operations are instant and don't add to time."
[OK] Correct: Even simple true/false checks happen for each item, so more items mean more time.
Understanding how logical checks scale helps you explain how your code handles bigger data clearly and confidently.
"What if we replaced the logical AND with a nested loop checking pairs of elements? How would the time complexity change?"
Practice
5 > 3 in MATLAB?Solution
Step 1: Understand the comparison operator
The operator>checks if the left number is greater than the right number.Step 2: Compare the numbers 5 and 3
Since 5 is greater than 3, the expression returnstrue.Final Answer:
true -> Option DQuick Check:
5 > 3 = true [OK]
- Confusing true with the number 5
- Thinking it returns the bigger number
- Mixing up > and < operators
Solution
Step 1: Evaluate each option
logical(0)returnsfalse.1 = 1causes syntax error (cannot assign to constant).trueerrors (function requires arguments liketrue()).1 == 1returnstrue.Step 2: Identify the working method
Only 1 == 1 correctly creates a logical true value.Final Answer:
1 == 1 -> Option AQuick Check:
1 == 1 = true [OK]
- logical(0) returns false
- Using = instead of == for comparison
- Omitting () on true()
val = (4 < 2) | (7 == 7); disp(val);
Solution
Step 1: Evaluate each logical expression
4 < 2is false (0),7 == 7is true (1).Step 2: Apply the OR operator (|)
False OR True results in true (1).Final Answer:
1 -> Option CQuick Check:
(false OR true) = true = 1 [OK]
- Confusing OR (|) with AND (&)
- Expecting vector output
- Misreading comparison operators
if 5 = 5
disp('Equal');
endSolution
Step 1: Check the if condition syntax
The single equal sign=is for assignment, not comparison.Step 2: Correct the comparison operator
Use double equal signs==to compare values in conditions.Final Answer:
Using single = instead of == in if condition -> Option AQuick Check:
Comparison needs ==, not = [OK]
- Using = in if condition
- Ignoring MATLAB error messages
- Assuming semicolon is mandatory after disp
A = [-1 0 1 2 0], which MATLAB expression returns a logical vector indicating which elements are nonzero?Solution
Step 1: Understand each expression
A ~= 0returns true where not zero.A != 0uses invalid != operator (use ~=).A > 0true only for positives.Step 2: Check which elements each marks true for
-A = [-1 0 1 2 0]A ~= 0: [true, false, true, true, false]
-A != 0: syntax error
-A > 0: [false, false, true, true, false]Final Answer:
A ~= 0 -> Option BQuick Check:
A ~= 0 = nonzero logical vector [OK]
- Using != instead of ~=
- Confusing ~= with ==
- Assuming >0 misses negative nonzero
Ready to prove it?
Pass the test and this pattern moves unknown
