Descriptive Statistics

If you need more advanced statistics functionality, you might want to use the Statistics Toolbox™ software. For more information see the Statistics Toolbox documentation.

Functions for Calculating Descriptive Statistics

You can use the following MATLAB® functions to calculate the descriptive statistics for your data.

Statistics Function Summary

Function

Description

max

Maximum value

mean

Average or mean value

median

Median value

min

Smallest value

mode

Most frequent value

std

Standard deviation

var

Variance, which measures the spread or dispersion of the values

The following examples apply MATLAB functions to calculate descriptive statistics:

Example 1 — Calculating Maximum, Mean, and Standard Deviation

This example shows how to use MATLAB functions to calculate the maximum, mean, and standard deviation values for a 24-by-3 matrix called count. MATLAB computes these statistics independently for each column in the matrix.

% Load the sample data
load count.dat
% Find the maximum value in each column
mx = max(count)
% Calculate the mean of each column
mu = mean(count)
% Calculate the standard deviation of each column
sigma = std(count)

The results are

mx =
          114          145          257

mu = 
      32.0000      46.5417      65.5833

sigma =
      25.3703      41.4057      68.0281

To get the row numbers where the maximum data values occur in each data column, you can specify a second output parameter indx to return the row index. For example:

[mx,indx] = max(count)

These results are

mx =
      114     145     257

indx =
      20    20    20

Here, the variable mx is a row vector that contains the maximum value in each of the three data columns. The variable indx contains the row indices in each column that correspond to the maximum values.

To find the minimum value in the entire count matrix, you can reshape this 24-by-3 matrix into a 72-by-1 column vector by using the syntax count(:). Then, to find the minimum value in the single column, you can use the following syntax:

min(count(:))

ans =
      7

Example 2 — Subtracting the Mean

You can subtract the mean from each column of the matrix by using the following syntax:

% Get the size of the count matrix
[n,p] = size(count)
% Compute the mean of each column
mu = mean(count)
% Create a matrix of mean values by
% replicating the mu vector for n rows
MeanMat = repmat(mu,n,1)
% Subtract the column mean from each element
% in that column
x = count - MeanMat

Example: Using MATLAB® Data Statistics

The Data Statistics dialog box helps you calculate and plot descriptive statistics with the data. This example shows how to use MATLAB Data Statistics to calculate and plot statistics for a 24-by-3 matrix, called count.

This section contains the following topics:

Calculating and Plotting Descriptive Statistics

  1. Load and plot the data:

    load count.dat
    [n,p] = size(count);
    % Define the x-values
    t = 1:n;
    % Plot the data and annotate the graph
    plot(t,count)
    legend('Location 1','Location 2','Location 3',2)
    xlabel('Time'), ylabel('Vehicle Count')
    

  2. In the Figure window, select Tools > Data Statistics .

    This opens the Data Statistics dialog box, which displays descriptive statistics for the X- and Y-data of the Location 1 data set.

  3. Select a different data set in the Statistics for list: Location 2.

    This displays the statistics for the X and Y data of the Location 2 data set.

  4. Select the check box for each statistic you want to display on the plot.

    For example, to plot the mean of Location 2, select the mean check box in the Y column.

    This plots a horizontal line to represent the mean of Location 2 and updates the plot legend to include this statistic.

Formatting Data Statistics on Plots

The Data Statistics GUI uses colors and line styles to distinguish statistics from the data on the plot. This portion of the example shows how to customize the display of descriptive statistics on a plot, such as the color, line width, line style, or marker.

To modify the display of data statistics on a plot:

  1. In the MATLAB Figure window, click the (Edit Plot) button in the toolbar.

    This enables plot editing.

  2. Double-click the statistic on the plot for which you want to edit display properties. For example, double-click the horizontal line representing the mean of Location 2.

    This opens the Property Editor below the MATLAB Figure window, where you can modify the appearance of the line used to represent this statistic.

  3. In the Property Editor, specify the Line and Marker styles, sizes, and colors.

Saving Statistics to the MATLAB® Workspace

This portion of the example shows how to save statistics in the Data Statistics GUI to the MATLAB workspace.

  1. In the Data Statistics dialog box, click the Save to workspace button.

  2. In the Save Statistics to Workspace dialog box, specify to save statistics for either X data, Y data, or both. Then, enter the corresponding variable names.

    In this example, save only the Y data. Enter the variable name as Loc2countstats.

  3. Click OK.

    This saves the descriptive statistics to a structure. The new variable is added to the MATLAB workspace.

To view the new structure variable, type the variable name at the MATLAB prompt:

Loc2countstats

Loc2countstats = 

       min: 9
       max: 145
      mean: 46.5417
    median: 36
      mode: 9
       std: 41.4057
     range: 136

Generating an M-file

This portion of the example shows how to generate an M-file that reproduces the format of the plot and the plotted statistics with new data.

  1. In the Figure window, select File > Generate M-File.

    This creates a function M-file and displays it in the MATLAB Editor. The code in the M-file shows you how to programmatically reproduce what you did interactively with the Data Statistics GUI and the Property Editor.

  2. Change the name of the function on the first line of the M-file from createfigure to something more specific, like countplot. Save the file to your current directory with the file name countplot.m.

  3. Generate some new, random count data:

    randcount = 300*rand(24,3);
  4. Reproduce the plot with the new data and the recomputed statistics:

    countplot(t,randcount)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS