📖 Scenario: You work in a small grocery store. You have a list of product prices, but you want to find only the products that cost more than a certain amount to plan special discounts.
🎯 Goal: Build a MATLAB program that filters a list of product prices to keep only those prices above a given threshold.
📋 What You'll Learn
Create a vector with exact product prices
Create a threshold variable for filtering
Use logical indexing to filter prices above the threshold
Display the filtered prices
💡 Why This Matters
🌍 Real World
Filtering prices helps stores decide which products to discount or promote based on cost.
💼 Career
Data filtering is a common task in data analysis, quality control, and reporting jobs.
Progress0 / 4 steps
1
Create the product prices vector
Create a vector called prices with these exact values: [12.5, 7.8, 15.0, 3.2, 9.9]
MATLAB
Hint
Use square brackets [] to create a vector in MATLAB.
2
Set the price threshold
Create a variable called threshold and set it to 10
MATLAB
Hint
Just assign the number 10 to the variable threshold.
3
Filter prices above the threshold
Create a new vector called filtered_prices that contains only the values from prices greater than threshold using logical indexing
MATLAB
Hint
Use prices(prices > threshold) to select prices above the threshold.
4
Display the filtered prices
Use disp to display the filtered_prices vector
MATLAB
Hint
Use disp(filtered_prices) to show the filtered prices on the screen.
Practice
(1/5)
1. What does filtering an array in MATLAB mean? filtered = array(condition);
easy
A. Selecting elements that meet a condition
B. Sorting the array elements
C. Changing the array size randomly
D. Adding new elements to the array
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 syntax array(condition) returns elements where the condition is true.
Final Answer:
Selecting elements that meet a condition -> Option A
Quick Check:
Filtering = Selecting elements [OK]
Hint: Filtering picks elements matching condition inside parentheses [OK]
Common Mistakes:
Thinking filtering sorts the array
Believing filtering changes array size randomly
Confusing filtering with adding elements
2. Which of the following is the correct MATLAB syntax to filter array A for values greater than 10?
easy
A. filtered = A > 10;
B. filtered = A{A > 10};
C. filtered = A(A > 10);
D. filtered = filter(A > 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); uses A(A > 10), which correctly filters elements greater than 10.
Final Answer:
filtered = A(A > 10); -> Option C
Quick Check:
Use parentheses with condition for filtering [OK]
Hint: Use parentheses with condition to filter arrays [OK]
Common Mistakes:
Using > without parentheses returns logical array, not filtered values