KAHIBARO Bookshelves
Discord Login Register

16.1. Descriptive Statistics and Summary Measures

Understanding Descriptive Statistics

Descriptive statistics summarize and describe the main features of a data set using numbers. Instead of looking at every single value, you use a few measures to capture typical behavior, spread, and shape. In MATLAB, these summaries are usually computed from vectors, tables, or columns of data.

You will mainly work with numeric arrays, for example a vector x that contains measurements. Many descriptive functions in MATLAB either have dedicated functions or can be accessed through mean, median, std, and related commands.

Measures of Central Tendency

Central tendency describes where the "center" of the data lies. MATLAB provides several ways to compute this, each capturing a different notion of the center.

The most common measure is the arithmetic mean. For a vector $x$ with $n$ elements, the mean is
$$
\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i.
$$
In MATLAB, you compute this with:

matlab
m = mean(x);

The result m is a single scalar that represents the average value of x.

The median is the middle value of the data when sorted. It is less sensitive to extreme values than the mean. For a vector x:

matlab
m_med = median(x);

If the number of elements is odd, the median is the central element after sorting. If it is even, the median is the average of the two central elements.

Sometimes you want to know the most frequent value, called the mode. MATLAB computes this with:

matlab
m_mode = mode(x);

The mode is useful when the data contain repeated discrete values, for example survey responses or counts. For continuous numeric measurements, the mode can be less informative because exact repeated values may be rare.

Measures of Spread or Variability

Spread describes how much the data vary around the center. Two data sets can have the same mean but very different variability.

A basic measure is the range. For a vector x, the range is
$$
\text{range}(x) = \max(x) - \min(x).
$$
In MATLAB there is a function range:

matlab
r = range(x);

You can also compute max(x) and min(x) individually if you want the extremes themselves.

Variance and standard deviation describe how far values tend to deviate from the mean. For a sample vector $x$ with $n$ values and mean $\bar{x}$, the sample variance is
$$
s^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2.
$$
The corresponding standard deviation is
$$
s = \sqrt{s^2}.
$$
In MATLAB, by default, var and std compute the sample versions:

matlab
v = var(x);
s = std(x);

If you need the population versions with denominator $n$ instead of $n - 1$, you can specify a second argument:

matlab
v_pop = var(x, 1);
s_pop = std(x, 1);

Larger standard deviation values indicate data that are more spread out around the mean.

Minimum, Maximum, and Percentiles

Extremes and percentiles describe where data lie in terms of their ordered positions. These are often reported together with central tendency and spread.

The minimum and maximum are direct:

matlab
x_min = min(x);
x_max = max(x);

They show the smallest and largest values in the dataset.

Percentiles divide ordered data into 100 equal parts. For example, the $p$th percentile is a value such that approximately $p$ percent of the data lie below it. In MATLAB, you compute percentiles using prctile:

matlab
p25 = prctile(x, 25);   % 25th percentile
p50 = prctile(x, 50);   % 50th percentile, often equal to the median
p75 = prctile(x, 75);   % 75th percentile

You can request multiple percentiles at once by passing a vector of percentages, for example prctile(x, [5 95]) to get the 5th and 95th percentiles.

Quartiles are special percentiles that cut the data into four equal parts. The first quartile $Q_1$ is the 25th percentile, the second quartile $Q_2$ is the 50th percentile, and the third quartile $Q_3$ is the 75th percentile. Quartiles are useful summaries of the distribution, especially when combined into the interquartile range.

Interquartile Range and Robust Spread

The interquartile range (IQR) measures the spread of the middle 50 percent of the data. It is defined as
$$
\text{IQR} = Q_3 - Q_1.
$$
In MATLAB, there is a dedicated function:

matlab
iq = iqr(x);

Because the IQR ignores the lowest 25 percent and highest 25 percent of values, it is less affected by extreme values than the full range or standard deviation.

You can also access quartiles using prctile, then compute the IQR manually if you wish:

matlab
q = prctile(x, [25 75]);
iq_manual = q(2) - q(1);

Robust measures like the median and IQR are often preferred when data contain outliers.

Shape of the Distribution: Skewness and Kurtosis

Beyond center and spread, you might want to capture the shape of the distribution. Two common shape measures are skewness and kurtosis.

Skewness measures asymmetry. A distribution with positive skewness has a longer tail to the right. A distribution with negative skewness has a longer tail to the left. In MATLAB:

matlab
sk = skewness(x);

This returns a number that is close to zero for roughly symmetric data.

Kurtosis measures how heavy the tails are compared to a normal distribution. Larger kurtosis often indicates more extreme values in the tails. In MATLAB:

matlab
kt = kurtosis(x);

The exact definition of kurtosis used depends on the options, but you can usually interpret higher values as heavier tails or more extreme outliers.

These measures are more advanced summaries. They are useful when you need a compact description of distribution shape without plotting, for example when comparing different datasets programmatically.

Working with Vectors vs Matrices

Most descriptive statistics functions operate along a chosen dimension for matrices or higher dimensional arrays. If X is a matrix, mean(X) by default returns a row vector containing the mean of each column. Similarly, std(X) returns standard deviations for each column.

If you want summaries along a different dimension, you can specify it explicitly. For example, to compute the mean of each row in X, use:

matlab
rowMean = mean(X, 2);

The same dimension argument pattern applies to many functions used in descriptive statistics, such as median, var, min, max, prctile, and iqr.

When you are working with tables, some functions can operate directly on table variables, but often you extract a table column into a vector, for instance X = T.Height;, then apply the usual numeric functions.

Dealing with Missing Data in Summaries

Real datasets often contain missing values represented as NaN. Many basic functions treat NaN as a regular number, which can spoil your summaries. For example, mean([1 2 NaN]) returns NaN. To ignore missing values, MATLAB provides nan versions of several functions.

For the mean, variance, and standard deviation, typical usage looks like:

matlab
m = nanmean(x);
s = nanstd(x);
v = nanvar(x);

For other summaries, you can often supply an option that tells the function to omit missing values. For example:

matlab
m2 = mean(x, 'omitnan');
p = prctile(x, 50, 'all', 'omitnan');

Here, the 'omitnan' flag instructs MATLAB to compute the summary ignoring NaN entries. The specific syntax can vary among functions, so checking the function documentation for handling missing values is useful.

Careful handling of missing data is essential, especially if you want summaries that reflect the observed information rather than just returning NaN.

Quick Combined Summaries

When you need several descriptive measures at once, using a single function can be convenient. MATLAB provides summary behavior for tables and some higher level functions in toolboxes, but for numeric arrays a common pattern is to compute a small set of core summaries together.

A typical snippet to summarize a vector x might be:

matlab
results.mean      = mean(x, 'omitnan');
results.median    = median(x, 'omitnan');
results.min       = min(x);
results.max       = max(x);
results.std       = std(x, 'omitnan');
results.iqr       = iqr(x);
results.skewness  = skewness(x);
results.kurtosis  = kurtosis(x);

This creates a structure containing several summary statistics that you can inspect or save. Such combined summaries are often the starting point for further analysis, plotting, or reporting.

Key points to remember:
Descriptive statistics summarize a dataset with a few numbers, not by examining every value.
Central tendency measures include mean, median, and mode. They describe "typical" values.
Spread measures such as range, variance, standard deviation, and interquartile range describe variability.
Percentiles and quartiles show where values lie in the ordered data. The interquartile range is $Q_3 - Q_1$.
Functions like mean, median, std, var, min, max, prctile, iqr, skewness, and kurtosis provide standard summaries in MATLAB.
Be careful with missing values NaN. Use options like 'omitnan' or nan variants of functions so that summaries are not dominated by missing data.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Getting Started with MATLAB
    • ☰ 1.1. What Is MATLAB and What Is It Used For
      • ☰ 1.2. Installing MATLAB and Setting Up the Environment
        • ☰ 1.3. Understanding the MATLAB Desktop Interface
          • ☰ 1.4. Command Window, Workspace, and Current Folder
            • ☰ 1.5. Basic Workflow and Running Simple Commands
              • ☰ 1.6. Getting Help and Documentation
              • ☰ 2. MATLAB Language Basics
                • ☰ 2.1. MATLAB Syntax and Expressions
                  • ☰ 2.2. Variables and Assignment
                    • ☰ 2.3. Data Types and Type Conversion
                      • ☰ 2.4. Vectors and Matrices Fundamentals
                        • ☰ 2.5. Creating and Indexing Arrays
                          • ☰ 2.6. Basic Arithmetic and Elementwise Operations
                            • ☰ 2.7. Using Built-In Constants and Functions
                            • ☰ 3. Working with Variables and the Workspace
                              • ☰ 3.1. Creating, Viewing, and Editing Variables
                                • ☰ 3.2. Workspace Browser and Variable Inspector
                                  • ☰ 3.3. Saving and Loading Data Files
                                    • ☰ 3.4. MAT-Files and Importing External Data
                                      • ☰ 3.5. Clearing Variables and Managing the Workspace
                                      • ☰ 4. Vectors, Matrices, and Arrays
                                        • ☰ 4.1. Row and Column Vectors
                                          • ☰ 4.2. Matrix Creation and Concatenation
                                            • ☰ 4.3. Indexing, Slicing, and Logical Indexing
                                              • ☰ 4.4. Matrix Operations vs Elementwise Operations
                                                • ☰ 4.5. Reshaping and Rearranging Arrays
                                                  • ☰ 4.6. Special Matrices and Functions
                                                  • ☰ 5. Basic Programming Constructs
                                                    • ☰ 5.1. Scripts vs Functions
                                                      • ☰ 5.2. Control Flow with if, elseif, else
                                                        • ☰ 5.3. Loops with for and while
                                                          • ☰ 5.4. break, continue, and return
                                                            • ☰ 5.5. Logical Expressions and Relational Operators
                                                              • ☰ 5.6. Best Practices for Clear and Readable Code
                                                              • ☰ 6. Functions in MATLAB
                                                                • ☰ 6.1. Creating Simple Functions
                                                                  • ☰ 6.2. Function Inputs and Outputs
                                                                    • ☰ 6.3. Function Files and the Path
                                                                      • ☰ 6.4. Local vs Global Variables
                                                                        • ☰ 6.5. Anonymous Functions
                                                                          • ☰ 6.6. Organizing Code into Multiple Functions
                                                                          • ☰ 7. Working with Numbers and Math
                                                                            • ☰ 7.1. Scalars and Vectorized Computations
                                                                              • ☰ 7.2. Basic Mathematical Functions
                                                                                • ☰ 7.3. Trigonometric and Exponential Functions
                                                                                  • ☰ 7.4. Complex Numbers in MATLAB
                                                                                    • ☰ 7.5. Rounding, Modulo, and Special Values
                                                                                      • ☰ 7.6. Numerical Precision and Floating Point Considerations
                                                                                      • ☰ 8. Strings, Characters, and Categorical Data
                                                                                        • ☰ 8.1. Character Arrays and String Arrays
                                                                                          • ☰ 8.2. Creating and Manipulating Strings
                                                                                            • ☰ 8.3. Converting Between Data Types and Strings
                                                                                              • ☰ 8.4. Comparing and Searching Strings
                                                                                                • ☰ 8.5. Categorical Arrays Basics
                                                                                                • ☰ 9. Tables, Structures, and Cell Arrays
                                                                                                  • ☰ 9.1. Introduction to Tables
                                                                                                    • ☰ 9.2. Creating Tables and Accessing Data
                                                                                                      • ☰ 9.3. Modifying and Joining Tables
                                                                                                        • ☰ 9.4. Structures and Nested Structures
                                                                                                          • ☰ 9.5. Cell Arrays and Cell Indexing
                                                                                                            • ☰ 9.6. Choosing the Right Container Type
                                                                                                            • ☰ 10. Importing and Exporting Data
                                                                                                              • ☰ 10.1. Reading Text and CSV Files
                                                                                                                • ☰ 10.2. Importing Excel Spreadsheets
                                                                                                                  • ☰ 10.3. Importing Data with the Import Tool
                                                                                                                    • ☰ 10.4. Exporting Data to Files
                                                                                                                      • ☰ 10.5. Working with Image and Audio Files
                                                                                                                        • ☰ 10.6. Basic File Management from MATLAB
                                                                                                                        • ☰ 11. Plotting and Visualization
                                                                                                                          • ☰ 11.1. Introduction to 2D Plotting
                                                                                                                            • ☰ 11.2. Customizing Plots and Axes
                                                                                                                              • ☰ 11.3. Multiple Plots in One Figure
                                                                                                                                • ☰ 11.4. Labels, Titles, Legends, and Annotations
                                                                                                                                  • ☰ 11.5. Plotting with Different Line and Marker Styles
                                                                                                                                    • ☰ 11.6. Saving and Exporting Figures
                                                                                                                                    • ☰ 12. Advanced Plotting for Beginners
                                                                                                                                      • ☰ 12.1. Subplots and Tiled Layouts
                                                                                                                                        • ☰ 12.2. Bar, Scatter, and Stem Plots
                                                                                                                                          • ☰ 12.3. Histograms and Density Plots
                                                                                                                                            • ☰ 12.4. 3D Plots and Surface Plots
                                                                                                                                              • ☰ 12.5. Using plottools and Interactive Editing
                                                                                                                                                • ☰ 12.6. Basics of Handle Graphics
                                                                                                                                                • ☰ 13. Working with Scripts and Live Scripts
                                                                                                                                                  • ☰ 13.1. Creating and Running Script Files
                                                                                                                                                    • ☰ 13.2. Sectioning Code and Running Sections
                                                                                                                                                      • ☰ 13.3. Live Scripts and Rich Text
                                                                                                                                                        • ☰ 13.4. Adding Equations, Images, and Outputs
                                                                                                                                                          • ☰ 13.5. Sharing and Exporting Live Scripts
                                                                                                                                                          • ☰ 14. Debugging and Code Quality
                                                                                                                                                            • ☰ 14.1. Finding and Fixing Syntax Errors
                                                                                                                                                              • ☰ 14.2. Setting Breakpoints and Stepping Through Code
                                                                                                                                                                • ☰ 14.3. Inspecting Variable Values During Execution
                                                                                                                                                                  • ☰ 14.4. Using the Editor Debugging Tools
                                                                                                                                                                    • ☰ 14.5. Using Code Analyzer and Warnings
                                                                                                                                                                      • ☰ 14.6. Writing Clean and Maintainable MATLAB Code
                                                                                                                                                                      • ☰ 15. Basic Linear Algebra with MATLAB
                                                                                                                                                                        • ☰ 15.1. Vectors and Norms
                                                                                                                                                                          • ☰ 15.2. Matrix Addition and Multiplication
                                                                                                                                                                            • ☰ 15.3. Solving Linear Systems with Backslash
                                                                                                                                                                              • ☰ 15.4. Inverses, Determinants, and Rank
                                                                                                                                                                                • ☰ 15.5. Eigenvalues and Eigenvectors
                                                                                                                                                                                  • ☰ 15.6. Using Linear Algebra Functions Safely
                                                                                                                                                                                  • ☰ 16. Data Analysis and Statistics Fundamentals
                                                                                                                                                                                    • ☰ 16.1. Descriptive Statistics and Summary Measures
                                                                                                                                                                                      • ☰ 16.2. Sorting, Filtering, and Grouping Data
                                                                                                                                                                                        • ☰ 16.3. Basic Correlation and Regression
                                                                                                                                                                                          • ☰ 16.4. Simple Data Cleaning Techniques
                                                                                                                                                                                            • ☰ 16.5. Basic Time Series Handling
                                                                                                                                                                                            • ☰ 17. Working with Time and Dates
                                                                                                                                                                                              • ☰ 17.1. Representing Dates and Times
                                                                                                                                                                                                • ☰ 17.2. Creating and Formatting datetime Arrays
                                                                                                                                                                                                  • ☰ 17.3. Date Calculations and Differences
                                                                                                                                                                                                    • ☰ 17.4. Plotting Data over Time
                                                                                                                                                                                                    • ☰ 18. Introduction to Signal and Image Handling
                                                                                                                                                                                                      • ☰ 18.1. Basic Signal Representation in MATLAB
                                                                                                                                                                                                        • ☰ 18.2. Simple Filtering and Transformations
                                                                                                                                                                                                          • ☰ 18.3. Reading, Displaying, and Writing Images
                                                                                                                                                                                                            • ☰ 18.4. Basic Image Transformations
                                                                                                                                                                                                            • ☰ 19. Automation and Scripting Tasks
                                                                                                                                                                                                              • ☰ 19.1. Batch Processing of Files
                                                                                                                                                                                                                • ☰ 19.2. Writing Reusable Utility Scripts
                                                                                                                                                                                                                  • ☰ 19.3. Using Relative Paths and the MATLAB Path
                                                                                                                                                                                                                    • ☰ 19.4. Simple User Interaction with input and disp
                                                                                                                                                                                                                    • ☰ 20. Introduction to Toolboxes
                                                                                                                                                                                                                      • ☰ 20.1. What Are Toolboxes and How to Use Them
                                                                                                                                                                                                                        • ☰ 20.2. Exploring Installed Toolboxes
                                                                                                                                                                                                                          • ☰ 20.3. Finding Functions in Toolboxes
                                                                                                                                                                                                                            • ☰ 20.4. Add-Ons and MATLAB File Exchange
                                                                                                                                                                                                                            • ☰ 21. Best Practices and Next Steps
                                                                                                                                                                                                                              • ☰ 21.1. Organizing Projects and Folders
                                                                                                                                                                                                                                • ☰ 21.2. Version Control Basics with MATLAB
                                                                                                                                                                                                                                  • ☰ 21.3. Performance Tips for Beginners
                                                                                                                                                                                                                                    • ☰ 21.4. Learning Resources and Documentation Path
                                                                                                                                                                                                                                      • ☰ 21.5. Suggested Practice Projects for Beginners