📖 Scenario: You are analyzing the daily sales numbers of a small store for one week. You want to understand the average sales, the middle value of sales, and how much the sales vary day to day.
🎯 Goal: Calculate the mean, median, and standard deviation of the daily sales data using MATLAB.
📋 What You'll Learn
Create a vector with the exact daily sales numbers
Create a variable to hold the number of days
Calculate mean, median, and standard deviation using MATLAB functions
Display the results clearly
💡 Why This Matters
🌍 Real World
Descriptive statistics help summarize and understand data quickly, such as daily sales, temperatures, or test scores.
💼 Career
Data analysts and scientists use these statistics to report insights and make decisions based on data patterns.
Progress0 / 4 steps
1
Create the daily sales data vector
Create a vector called sales with these exact daily sales numbers: [150, 200, 170, 220, 180, 210, 190]
MATLAB
Hint
Use square brackets [] to create a vector in MATLAB.
2
Create a variable for the number of days
Create a variable called num_days that stores the number of days by calculating the length of the sales vector using the length function.
MATLAB
Hint
Use length(sales) to find how many elements are in the vector.
3
Calculate mean, median, and standard deviation
Calculate the mean of sales and store it in mean_sales. Calculate the median of sales and store it in median_sales. Calculate the standard deviation of sales and store it in std_sales. Use MATLAB functions mean, median, and std respectively.
MATLAB
Hint
Use mean(sales), median(sales), and std(sales) to calculate the statistics.
4
Display the results
Use fprintf to print the mean, median, and standard deviation values with descriptive text. Print each value on its own line with two decimal places.
MATLAB
Hint
Use fprintf with format '%.2f' to show two decimal places.
Practice
(1/5)
1. What does the mean function calculate in MATLAB when applied to a numeric array?
easy
A. The difference between the largest and smallest values
B. The middle value after sorting the array
C. The total sum of all elements in the array
D. The average value of all elements in the array
Solution
Step 1: Understand the mean function
The mean function calculates the average by adding all elements and dividing by the number of elements.
Step 2: Compare with other statistics
Median is the middle value, not the average. Sum is total, not average. Range is difference, not average.
Final Answer:
The average value of all elements in the array -> Option D
Quick Check:
Mean = Average [OK]
Hint: Mean is the sum divided by count [OK]
Common Mistakes:
Confusing mean with median
Thinking mean is the sum only
Mixing mean with range
2. Which of the following is the correct MATLAB syntax to calculate the standard deviation of a vector data?
easy
A. median(data)
B. standard_deviation(data)
C. std(data)
D. mean(data)
Solution
Step 1: Recall MATLAB function names
MATLAB uses std() to calculate standard deviation, not standard_deviation().
Step 2: Eliminate incorrect options
mean() calculates average, median() calculates middle value, so both are incorrect for standard deviation.
Final Answer:
std(data) -> Option C
Quick Check:
Standard deviation function is std() [OK]
Hint: Use std() for standard deviation in MATLAB [OK]
Common Mistakes:
Using mean() instead of std()
Typing non-existent function standard_deviation()
Confusing median() with std()
3. What is the output of the following MATLAB code?
data = [3, 7, 5, 9, 1];
m = median(data);
s = std(data);
[m, s]
medium
A. [5, 2.8284]
B. [5, 3.1623]
C. [7, 3.1623]
D. [3, 2.8284]
Solution
Step 1: Calculate the median of data
Sorted data is [1, 3, 5, 7, 9]. The middle value is 5, so median = 5.
Step 2: Calculate the standard deviation
Mean = (3+7+5+9+1)/5 = 5.
Differences squared: (3-5)^2=4, (7-5)^2=4, (5-5)^2=0, (9-5)^2=16, (1-5)^2=16.
Sum = 40, variance = 40/4 = 10 (using n-1 for std), std = sqrt(10) ≈ 3.1623.
Final Answer:
[5, 3.1623] -> Option B
Quick Check:
Median = 5, Std ≈ 3.16 [OK]
Hint: Median is middle; std uses n-1 in denominator [OK]
Common Mistakes:
Using n instead of n-1 for std denominator
Mixing mean and median values
Incorrectly sorting data for median
4. Identify the error in this MATLAB code snippet that calculates the mean and median of values:
A. Missing closing parenthesis in median function call
B. Using mean instead of median for median_val
C. Incorrect variable name 'values'
D. No error, code runs fine
Solution
Step 1: Check syntax of median function call
The line median_val = median(values is missing a closing parenthesis.
Step 2: Confirm other parts are correct
mean(values) is correct, variable name is consistent, so no other errors.
Final Answer:
Missing closing parenthesis in median function call -> Option A
Quick Check:
Syntax error due to missing ')' [OK]
Hint: Check all parentheses in function calls [OK]
Common Mistakes:
Ignoring missing parenthesis errors
Confusing mean and median functions
Assuming variable name errors without checking
5. Given a dataset data = [2, 4, 4, 4, 5, 5, 7, 9], which MATLAB code correctly calculates the mean, median, and standard deviation and stores them in variables m, med, s respectively?
hard
A. m = mean(data); med = median(data); s = std(data);
B. m = median(data); med = mean(data); s = std(data);
C. m = std(data); med = mean(data); s = median(data);
D. m = mean(data); med = std(data); s = median(data);
Solution
Step 1: Match variables to correct functions
Variable m should hold mean, med median, s standard deviation.
Step 2: Verify correct function usage
m = mean(data); med = median(data); s = std(data); assigns mean to m, median to med, std to s, which is correct.
Final Answer:
m = mean(data); med = median(data); s = std(data); -> Option A
Quick Check:
Correct variable-function mapping [OK]
Hint: Assign mean, median, std to matching variable names [OK]
Common Mistakes:
Swapping mean and median assignments
Assigning std to median variable
Mixing variable names and functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.