📖 Scenario: You work in a small store and have a list of product prices. You want to find which products are expensive, meaning their price is above a certain amount.
🎯 Goal: Learn how to use logical indexing in MATLAB to select and display only the expensive products from a list of prices.
📋 What You'll Learn
Create a vector of product prices
Create a threshold price variable
Use logical indexing to find prices above the threshold
Display the filtered expensive prices
💡 Why This Matters
🌍 Real World
Stores often need to filter products by price to create sales or promotions.
💼 Career
Logical indexing is a key skill in data analysis and helps quickly select data based on conditions.
Progress0 / 4 steps
1
Create the product prices vector
Create a vector called prices with these exact values: [10, 25, 30, 15, 50, 5]
MATLAB
Hint
Use square brackets to create a vector in MATLAB.
2
Set the threshold price
Create a variable called threshold and set it to 20
MATLAB
Hint
Just assign the number 20 to the variable threshold.
3
Use logical indexing to find expensive products
Create a variable called expensive that uses logical indexing on prices to select only prices greater than threshold
MATLAB
Hint
Use prices > threshold inside parentheses to select elements.
4
Display the expensive products
Write a disp statement to display the variable expensive
MATLAB
Hint
Use disp(expensive) to show the filtered prices.
Practice
(1/5)
1. What does logical indexing in MATLAB allow you to do with an array A and a logical array L of the same size?
easy
A. Sort the elements of A based on L
B. Select elements of A where L is true
C. Change the size of A to match L
D. Convert A into a logical array
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 A and L
Elements of A are selected only where L has true values, filtering A.
Final Answer:
Select elements of A where L is true -> Option B
Quick Check:
Logical indexing = select elements [OK]
Hint: Logical array true picks elements from original array [OK]