9.2: MATLAB Statistics Functions
- Page ID
- 83703
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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
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)
- Answer
-
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
Video Links
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,
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.
- Answer
-
Add texts here. Do not delete this text first.
.