Logical indexing in MATLAB - Time & Space Complexity
Logical indexing lets us pick elements from an array using true or false values. We want to know how the time to do this changes as the array gets bigger.
How does the work grow when we select elements this way?
Analyze the time complexity of the following code snippet.
A = rand(1, n); % Create an array of n random numbers
idx = A > 0.5; % Logical array where elements > 0.5 are true
B = A(idx); % Select elements where idx is true
This code creates an array, finds which elements are bigger than 0.5, and then selects those elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element of A to see if it is greater than 0.5.
- How many times: Once for each element in the array, so n times.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows directly with the size of the array. Double the size, double the work.
Time Complexity: O(n)
This means the time to select elements grows in a straight line with the number of elements.
[X] Wrong: "Logical indexing is instant no matter how big the array is."
[OK] Correct: Even though logical indexing looks simple, MATLAB must check each element once, so bigger arrays take more time.
Understanding how logical indexing scales helps you write efficient code and explain your choices clearly in interviews. It shows you know how data size affects performance.
"What if we used a loop to select elements instead of logical indexing? How would the time complexity change?"
Practice
A and a logical array L of the same size?Solution
Step 1: Understand logical indexing concept
Logical indexing uses a logical array to pick elements from another array where the logical array is true.Step 2: Apply to arrays
Elements ofAandLAare selected only whereLhas true values, filteringA.Final Answer:
Select elements of A where L is true -> Option BQuick Check:
Logical indexing = select elements [OK]
- Thinking logical indexing changes array size arbitrarily
- Confusing logical indexing with sorting
- Assuming logical indexing converts data types
v that are greater than 5 using logical indexing?Solution
Step 1: Identify correct logical indexing syntax
In MATLAB, logical indexing uses parentheses with a logical condition inside, likev(condition).Step 2: Check each option
v(v > 5) usesv(v > 5), which is correct syntax. The other options use invalid indexing forms.Final Answer:
v(v > 5) -> Option AQuick Check:
Logical indexing syntax = v(condition) [OK]
- Using curly braces or square brackets for logical indexing
- Placing condition outside parentheses
- Confusing logical indexing with cell or array indexing
A = [3, 7, 2, 9, 5], what is the result of A(A > 4)?Solution
Step 1: Create logical array for condition A > 4
Check each element: 3>4?false, 7>4?true, 2>4?false, 9>4?true, 5>4?true. Logical array = [false, true, false, true, true].Step 2: Select elements of A where logical array is true
Elements at true positions: 7, 9, 5.Final Answer:
[7, 9, 5] -> Option DQuick Check:
Elements > 4 = [7, 9, 5] [OK]
- Including elements not greater than 4
- Confusing logical indexing with logical AND operation
- Returning original array instead of filtered
A = [1, 4, 6, 8];
L = [true, false];
B = A(L);
Solution
Step 1: Check sizes of A and L
A has 4 elements, L has 2 elements. Logical indexing requires L to be same size as A.Step 2: Identify error cause
Since sizes differ, MATLAB will give an error when using L to index A.Final Answer:
Logical array L size does not match A size -> Option AQuick Check:
Logical array size must match data size [OK]
- Assuming logical array can be smaller
- Thinking logical array must be numeric
- Believing logical indexing syntax is wrong here
M = [2, 5, 8; 1, 7, 3; 4, 6, 9]. How do you replace all elements less than 5 with zero using logical indexing?Solution
Step 1: Identify elements less than 5 in M
Elements less than 5 are 2, 1, 3, 4.Step 2: Use logical indexing to assign zero to these elements
UsingM(M < 5) = 0;replaces all elements less than 5 with zero.Final Answer:
M(M < 5) = 0; -> Option CQuick Check:
Replace elements < 5 with 0 using M(M < 5) = 0 [OK]
- Replacing wrong elements by using > or == instead of <
- Using incorrect logical operators
- Trying to assign without logical indexing
Ready to prove it?
Pass the test and this pattern moves unknown
