# 9.2: MATLAB Statistics Functions

• • Carey Smith
• Oxnard College
$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

By Carey A. Smith

% Create these 3 vectors to illustrate the functions in this section:

v1 = [10, 11, 13, 15, 23];

v2 = [10, 11, 13, 15];

v3 = [10, 15, 23, 11, 13]; % Same values as v1, but in a different order

## mean(x)

%% mean(x) = average of a vector or of the columns of a matrix

v1_mean = mean(v1)

% 14.4000

## std(x)

%% std(x) = standard deviation of a vector or of the columns of a matrix

% The standard deviation is a measure of the spread of the data. v1_std = std(v1) 

% 5.1769

## min(x)

%% min(x) = minimum of a vector or the columns of a matrix

v1_min = min(v1)

% 10

v3_min = min(v3) % This demonstrates that the order of the elements doesn't matter for computing the minimum.

% 10

% Both the minimum value and its index can be obtained by specifying 2 outputs:

[v1_min, idx_min] = min(v1)

% v1_min = 10

% idx_min = 1

## max(x)

%% max(x) = maximum of a vector or the columns of a matrix

v1_max = max(v1)

% 23

v3_max = max(v3) % order doesn't matter

% 23

% Both the maximum value and its index can be obtained by specifying 2 outputs:

[v1_max, idx_max] = max(v1)

% v1_max = 23

% idx_max = 5

## sort(x)

v3_sort1 = sort(v3) % Sorts smallest to largest

% 10 11 13 15 23

v3_sort1 = sort(v3,'ascend') % Also sorts smallest to largest

% 10 11 13 15 23

v3_sort3 = sort(v3,'descend') % Sorts largest to smallest

% 23 15 13 11 10

##### Exercise $$\PageIndex{1}$$sort() function assignment

%% The following data is from the Camrosa Water District's 2020 Water Quality report.

% (1pt) Define this vector of chloride levels from imported water and from wells:

Chloride = [52 113 122 45 83 133];

% (2 pts) Use this form to determine both the maximum value and the index of the maximum:

[Mx, Idx_max] = max(Chloride) % Returns the index corresponding to the maximum value

% (1 pt) Verify that the index is correct with this line:

Chloride(Idx_max)

Add texts here. Do not delete this text first.

## median(x)

%% median(x) = middle value of a vector or matrix when the vector is sorted from low to high

v1_median = median(v1)

% 13

% When a vector has an even number of elements, then the median is the mean of the 2 middle values

v2 = [10, 11, 13, 15]

v2_median = median(v2)

% 12

## Statistics of Matrices

M = [ 1  2 5 5;

     -3 -1 0 1;

     -1  3 4 7]

% mean(M) = mean of each column

M_mean_cols = mean(M)

% -1.0000 1.3333 3.0000 4.3333

% mean(M') = mean of each row

M_mean_rows = mean(M') % mean of the transpose

% 3.2500 -0.7500 3.2500

% 2nd method (better) to compute the mean of the rows:

M_mean_rows = mean(M,2) % mean of the rows or transpose

% 3.2500

% -0.7500

% 3.2500

% To get the mean of the whole matrix use this form:

M_mean_all_1 = mean(M(:)) % The (:) treats all the data of a matrix as a single vector.

% 1.9167

% 2nd form:

M_mean_all_2 = mean(M,'all')

% 1.9167

% The min(), max(), and std() functions for a matrix work similarly the mean() function

## What Could Go Wrong

Caution: The following code can also be used to compute the mean of a all the elements in a matrix, because mean is a linear function, but it does not work for the std() function, because the standard deviation uses squares and a square root; it is not linear.

M_mean_all_3 = mean(mean(M))

% 1.9167

M_std_all = std(M(:)) % correct

% 2.9683

M_std_all_wrong = std(std(M)) % wrong

% 0.4975

Normal distribution standard Deviation - Explained and Visualized (Jeremy Jones)

A fun summary of the mean, medium and mode is in this video:

“Mean, Median, Mode, & Range” (Lazy Song Parody), Dylan Peters EDU,

##### Exercise $$\PageIndex{2}$$ Statistics functions assignment

a. Use the randn() function to generate a vector of 100 normally distributed random data values with a nominal mean of of 50 and a nominal standard deviation of 8.

b. Compute the mean, median, and standard deviation of the random vector.

Since randn() generates a different of random numbers every time it is called. So, rerun your code 4 more times to see how much the mean and standard deviation change. By the “Law of Large Numbers”, the more elements the vector has, then the closer the mean of the generated vector will be to the specified mean.