Filtering basics in MATLAB - Time & Space Complexity
When we filter data in MATLAB, we pick certain items from a list based on a rule.
We want to know how the time to do this grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
data = 1:1000;
filtered = [];
for i = 1:length(data)
if mod(data(i), 2) == 0
filtered(end+1) = data(i);
end
end
This code goes through each number in the list and keeps only the even numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that checks each number once.
- How many times: Exactly once for every item in the list.
As the list gets bigger, the code checks more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the list size.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the number of items.
[X] Wrong: "Filtering only a few items means the code runs faster than checking all items."
[OK] Correct: The code still looks at every item once, so time depends on total items, not how many pass the filter.
Understanding how filtering scales helps you explain how your code handles bigger data smoothly.
"What if we used a built-in MATLAB function like 'find' instead of a loop? How would the time complexity change?"
Practice
filtered = array(condition);Solution
Step 1: Understand filtering concept
Filtering means choosing only those elements from an array that satisfy a condition.Step 2: Analyze the MATLAB syntax
The syntaxarray(condition)returns elements where the condition is true.Final Answer:
Selecting elements that meet a condition -> Option AQuick Check:
Filtering = Selecting elements [OK]
- Thinking filtering sorts the array
- Believing filtering changes array size randomly
- Confusing filtering with adding elements
A for values greater than 10?Solution
Step 1: Identify correct filtering syntax
In MATLAB, filtering uses parentheses with a logical condition inside to select elements.Step 2: Check each option
filtered = A(A > 10); usesA(A > 10), which correctly filters elements greater than 10.Final Answer:
filtered = A(A > 10); -> Option CQuick Check:
Use parentheses with condition for filtering [OK]
- Using > without parentheses returns logical array, not filtered values
- Using curly braces {} which are for cell arrays
- Using undefined function 'filter'
A = [3, 12, 7, 15, 9];
filtered = A(A >= 10);
disp(filtered);
Solution
Step 1: Understand filtering condition
The conditionA >= 10selects elements in A that are 10 or greater.Step 2: Apply condition to array A
Elements 12 and 15 satisfy this condition, so filtered = [12 15].Final Answer:
[12 15] -> Option DQuick Check:
Elements >= 10 = [12 15] [OK]
- Including elements less than 10
- Expecting original array output
- Confusing logical array with filtered values
A = [2, 8, 4, 6];
filtered = A{A < 5};
Solution
Step 1: Check filtering syntax
Filtering arrays uses parentheses (), not curly braces {} which are for cell arrays.Step 2: Identify the error
UsingA{A < 5}causes an error because curly braces cannot index numeric arrays.Final Answer:
Using curly braces {} instead of parentheses () -> Option AQuick Check:
Use () for filtering numeric arrays [OK]
- Using {} for numeric array indexing
- Ignoring MATLAB error messages
- Confusing semicolon necessity
M = [1 5 9; 4 2 7; 8 3 6], how do you filter all elements greater than 4 and return them as a single column vector?Solution
Step 1: Apply filtering condition
UseM(M > 4)to select all elements greater than 4 from matrix M.Step 2: Convert result to column vector
Usefiltered(:)to reshape the filtered elements into a single column vector.Final Answer:
filtered = M(M > 4); filtered = filtered(:); -> Option BQuick Check:
Filter then reshape to column vector [OK]
- Using curly braces {} for filtering
- Trying to filter columns instead of elements
- Using undefined function 'filter'
Ready to prove it?
Pass the test and this pattern moves unknown
