12.3: Common Matrix Functions
- Page ID
- 85031
\( \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}\)Commands: size and length
Many functions involving matrices will only work if the dimensions of the matrices satisfy certain conditions. The size(M) command returns the number of rows and columns in M.
>> A = [1 2 3 4;
5 6 7 8];
>> size(A)
ans =
2 4
and
>> B = [1;
6;
0;
9];
>> size(B)
ans =
4 1
The size() command is also what is known as an “overloaded” function in that it can be used in a couple of ways. Suppose we only wanted to know only the number of rows in a matrix M. We could find the size(M), store this as a variable and then select the first entry. Instead, the size() command takes a second entry that will allow us to get what we want.
>> A = [1 2 3 4;
5 6 7 8];
>> size(A,1)
ans =
2
(the number of rows)
and
>> A = [1 2 3 4;
5 6 7 8];
>> size(A,2)
ans =
4
(the number of columns).
The length of a vector (either a row or column) is simply the number of elements in the vector:
>> c=1:5;
>> length(c)
ans =
5
However, the “length of a matrix” is defined in Matlab as the larger of the number of rows and the number of columns. That is, length(A) is equivalent to max(size(A)).
Commands: max and min
These two functions are (almost) self explanatory. For the matrix that we are using,
A = [1 2 3 4;
5 6 7 8],
if we try max(A), we get 5 6 7 8. What’s going on?
Remember that Matlab works on column precedence so that what max() is doing is not finding the maximum value of the entire matrix, but instead finding the maximums of each column. The only exception occurs when the starting matrix is either a row or column vector. For example, for B = [1; 6; 0; 9], max(B) does give us 9 (the largest value in B).
So, to get the largest element in A, we could “nest” the functions as max(max(A)), which would give us 8.
Or, we can use max() with the colon operator to treat the entire matrix as a vector: max(A(:)), which would also compute 8. The form max(A(:)) is preferred, because this form works with other functions such as std(A(:)) which give the correct standard deviation, while std(std(A)) gives an incorrect answer. [C.A.S.]
Commands: sum and prod
Once again, these seem reasonably named functions (see previous bullet). And once again, they return not the sum\product of every entry in matrix, but the column sums\products with the exception being for vectors, in which case you do get the sum\product of every entry in the vector. But, what if you want to get the sum along the rows? Well, once again sum is an overloaded operator and we can use:
>> A = [1 2 3 4;
5 6 7 8]
>> sum(A)
ans =
6 8 10 12
(the column sums
and
>> sum(A,2)% The "2" means sum in the 2nd dimension, that is, across the rows
ans =
10
26
(the row sums)
For either a row vector, V, or a column vector, V, sum(V) is the syum of the elements of V.
For a matrix, A, we have the following options when using sum():
sum(A) sums the columns (the "1st dimension")
sum(A, dim): dim means the direction of the sum:
When A is 2-dimensional:
sum(A, 1) is the sum of the columns.
sum(A, 2) is the sum of the rows.
To create a 3-dimensional matrix, read this link:
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html
.
Exercise
"Hilbert matrix" operations
Consider the 5 x 5 "Hilbert matrix"
H5 = [ 1 1/2 1/3 1/4 1/5;
1/2 1/3 1/4 1/5 1/6;
1/3 1/4 1/5 1/6 1/7;
1/4 1/5 1/6 1/7 1/8;
1/5 1/6 1/7 1/8 1/9];
a. Using a command in this section, find the dimensions of H5.
b. Using commands in this section, find the column sums of H5 and the row sums of H5.
c. Use the max() function to determine the value of the maximum entry of H5.
- Answer
-
Add texts here. Do not delete this text first.
.