\startcomponent context-matrices %\showframe \setupheadertexts[Matlab and Matrices] %\starttext \start \setupalign[middle] \tfc Matlab and Matrices \tfa\setupinterlinespace \vskip 2ex Math 45---Linear Algebra David Arnold \vskip 2ex \stop \midaligned{\currentdate} \vskip 2ex \midaligned{\bf Abstract} \startnarrower[3*middle] \noindenting In this exercise you will learn how to enter and edit matrices in Matlab. You will also experiment with some of Matlab's built-in matrix builders. You will also learn how to build matrices from vectors and blocks of matrices. {\em Prequisites: None} \stopnarrower \section{Entering Matrices} Entering matrices in Matlab is easy. Enter the following at the Matlab prompt. \starttyping >> A=[1,2,3;4,5,6;7,8,9] \stoptyping Delimit the entries in a row with commas and delimit the rows with semicolons. Spaces can also be used to delimit the entries in a row. \starttyping >> A=[1 2 3;4 5 6;7 8 9] \stoptyping \subsection{Special Matrices in Matlab} Matlab has a number of built-in routines to help create matrices \footnote{For a list of these elementary matrices, type \type{help elmat} and \type{help specmat} at the Matlab prompt.}. You can create a matrix of zeros of any size \footnote{Remember, help is readily available for any Matlab function. For example, type \type{help zeros} to receive help on the \type{zeros} function.}. \starttyping >> A=zeros(5) >> B=zeros(3,5) \stoptyping You easily can create a matrix of zeros that is the same size as a given matrix. \starttyping >> C=magic(5) >> D=zeros(size(C)) \stoptyping You can create matrices of ones in a similar manner. \starttyping >> A=ones(6) >> B=ones(2,10) >> C=hilb(5) >> D=ones(size(C)) \stoptyping When performing simulations in Matlab it is useful to build matrices of random numbers. You can create a matrix of uniformly distributed random numbers, each between 0 and 1 (0 inclusive), with the following commands. \starttyping >> A=rand(6) >> B=rand(5,3) \stoptyping Scalar multiplication works with matrices in exactly the same manner as with vectors. \starttyping >> C=10*rand(5) >> D=floor(10*rand(5)) \stoptyping The identity matrix has ones on its main diagonal and zeros everywhere else.\footnote{The import of the identity matrix will be investigated in later activities.} \starttyping >> I=eye(5) \stoptyping Other types of diagonal matrices are possible with Matlab's \type{diag} command. \starttyping >> E=diag([1,2,3,4,5]) >> F=diag([1,2,3,4,5],-1) >> G=diag(1:5,1) \stoptyping \subsection{The Transpose Operator} The transpose operator (single apostrophe) plays the same role with matrices as it does with vectors. Rows are changed to columns and columns are changed to rows. \starttyping >> J=[1 2 3;4 5 6;7 8 9] >> J' \stoptyping \subsection{Suppressing Output} Remember, appending a semicolon to any Matlab command will suppress the output. This is useful if the result is large and you have no desire to view it. \starttyping >> K=rand(100); \stoptyping \subsection{Matlab's Workspace} Take a moment to examine Matlab's workspace with the \type{whos} command. \starttyping >> whos \stoptyping Note the size of each of the variables in your workspace is given. Of course, you could also find the size of the matrix \type{I} by entering \starttyping >> size(I) \stoptyping \section{Indexing Matrices} The following notation is used to denote a matrix with 3 rows and 3 columns. \placeformula[-] \startformula A=\pmatrix{% a_{11} & a_{12} & a_{13} \cr a_{21} & a_{22} & a_{23} \cr a_{31} & a_{32} & a_{33}} \stopformula Note that $a_{12}$ refers to the entry \index{matrix entries@matrix entries} in row 1, column 2. In general, $a_{ij}$ refers to the entry in row $i$, column $j$. Matlab uses similar notation to identify the elements of a matrix. \starttyping >> A=pascal(5) >> A(1,2) >> A(3,4) \stoptyping In general, \type{A(i,j)} refers to the element in row $i$, column $j$ of matrix $A$. You can easily change an entry in a matrix. \starttyping >> A(3,3)=11111 \stoptyping \subsection{Advanced Indexing Techniques} When indexing a matrix, your subscripts can also be vectors. This is a powerful tool which allows you to easily select a submatrix of a given matrix. \starttyping >> A=magic(6) >> A([1,2],[3,4,5]) \stoptyping The notation \type{A([1,2],[3,4,5])} references the submatrix formed using the elements that appear in rows 1 and 2 {\em and} in columns 3,4, and 5 of the matrix \type{A}. The command \starttyping >> A([1,3,5],[1,2,3,4,5,6]) \stoptyping produces a submatrix composed of rows 1, 3, and 5 of matrix \type{A}. If you recall that the notation \type{1:6} represents the vector \type{[1,2,3,4,5,6]}, so that \type{A(1,1:6)} is equivalent to \type{A(1,[1,2,3,4,5,6])}. \starttyping >> A(1,1:6) \stoptyping Using a colon by itself in place of a subscript denotes {\em all} of the corresponding row or column. Thus, \starttyping >> A(:,1) \stoptyping produces the first column of the matrix \type{A}, and \starttyping >> A(3,:) \stoptyping produces the third row of the matrix \type{A}. In a sense, you can read the notation \type{A(3,:)} as follows: ``Third row, every column.'' The command \starttyping >> A(1:3,:) \stoptyping produces a submatrix composed of the first three rows of the matrix \type{A}, while the command \starttyping >> A(:,1:2:6) \stoptyping produces a submatrix composed of columns 1, 3, and 5 of matrix \type{A}. \section{Building Matrices} Matlab allows you to build complex matrices out of vectors and matrices. \subsection{Building Matrices From Vectors} Create three row vectors with the following commands. \starttyping >> v1=1:3 >> v2=4:6 >> v3=7:9 \stoptyping The command \starttyping >> M=[v1;v2;v3] \stoptyping builds a matrix with the vectors \type{v1}, \type{v2}, and \type{v3} each forming a row of the matrix \type{M}. The command \starttyping >> N=[v1,v2,v3] \stoptyping produces a significantly different but logical result. Change the vectors \type{v1}, \type{v2}, and \type{v3} into column vectors with the transpose operator. \starttyping >> v1=v1' >> v2=v2' >> v3=v3' \stoptyping The command \starttyping >> P=[v1,v2,v3] \stoptyping builds a matrix with the vectors \type{v1,v2}, and \type{v3} each forming a column of the matrix \type{P}. Of course, you could have achieved the same result by taking the transpose of the matrix \type{M}. \starttyping >> M' \stoptyping \subsection{The Dimensions Need To Match} When building matrices, be sure that each row and column has the same number of elements. For example, the following sequence of commands will produce an error. \starttyping >> w1=1:3,w2=4:6,w3=7:10 >> Q=[w1;w2;w3] \stoptyping \subsection{Building Matrices From Other Matrices} It is a simple matter to augment a row or column vector to a matrix. Remember, each row and column of a matrix must have the same number of elements. Thus, \starttyping >> A=[1,2,3,4;5,6,7,8;9,10,11,12] >> b=[1,1,1]' >> M=[A,b] \stoptyping is valid, but \starttyping >> M=[A;b] \stoptyping is not. You can also augment two or more matrices. Remember, each row and column of a matrix must have the same number of elements. Thus, \starttyping >> A=magic(3),B=ones(3,4)} >> M=[A,B] \stoptyping is valid, but \starttyping >> N=[A;B] \stoptyping is not. \subsection{Your Imagination Is Your Only Limit} Matlab's matrix building capability is extremely flexible. Here is an interesting example. \starttyping >> A=zeros(3),B=ones(3),C=2*ones(3),D=3*ones(3) >> M=[A,B;C,D] \stoptyping Here is something called a {\em Vandermonde} matrix. \starttyping >> x=[1,2,3,4,5]' >> N=[ones(size(x)),x,x.^2,x.^3,x.^4] \stoptyping And here is one final piece of chicanery. \starttyping >> B=zeros(8) >> B(1:3,1:3)=[1 2 3;4 5 6;7 8 9] >> B(4:8,4:8)=magic(5) \stoptyping %\stoptext \stopcomponent