0
0
MATLABdata~30 mins

Comparison operators in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Comparison operators
📖 Scenario: You are working in a store that sells fruits. You want to check which fruits have prices less than or equal to a certain budget.
🎯 Goal: Create a MATLAB script that uses comparison operators to find which fruits are affordable within a given budget.
📋 What You'll Learn
Create a dictionary (containers.Map) called fruitPrices with exact entries: 'apple' = 3, 'banana' = 1, 'cherry' = 5, 'date' = 2
Create a variable called budget and set it to 3
Use a for loop with variables fruit and price to iterate over fruitPrices.keys and check prices using comparison operators
Create a logical array called affordable that stores true for fruits with price less than or equal to budget
Print the names of fruits that are affordable using disp
💡 Why This Matters
🌍 Real World
Stores and shops often need to filter products based on price limits to help customers find affordable options.
💼 Career
Understanding comparison operators and data structures like dictionaries is essential for data filtering and decision-making in programming jobs.
Progress0 / 4 steps
1
Create the fruit prices dictionary
Create a containers.Map called fruitPrices with these exact entries: 'apple' = 3, 'banana' = 1, 'cherry' = 5, and 'date' = 2.
MATLAB
Need a hint?

Use containers.Map with two cell arrays: one for keys and one for values.

2
Set the budget variable
Create a variable called budget and set it to 3.
MATLAB
Need a hint?

Just assign the number 3 to the variable budget.

3
Check which fruits are affordable
Use a for loop with variable fruit to iterate over fruitPrices.keys. Inside the loop, get the price of each fruit from fruitPrices. Create a logical array called affordable that stores true if price is less than or equal to budget, otherwise false.
MATLAB
Need a hint?

Use fruitPrices.keys to get all fruits, then loop with an index. Compare each price with budget using <=.

4
Display affordable fruits
Use a for loop with index i to iterate over affordable. Inside the loop, if affordable(i) is true, use disp to print the fruit name from keysList{i}.
MATLAB
Need a hint?

Loop over affordable and print fruit names where the value is true.