2What will I learn in this lecture? How can you create matrices ?How do the back-slash \ and * operators work with matrices?Understand how to subscript matrices.Use the built-in functions eye , ones, zeros and diag to create matrices.Learn about the size function the matrix counterpart to the length vector functionReadings: Matlab by Pratap Chapters 2.3.1, 2.3.2, 5.1
3Arrays - Vectors and Matrices In Matlab (for CS101) all data will be stored in arrays.An array can represent a:vector - size 1 x n (row vector with 1 row and n columns) or n x 1 (column vector with n rows and 1 column)matrix - any size m x n (m rows and n columns).ConventionsTwo arrays are defined to be equal if they are of the same size and at each position, they have the same value.
4Creating a Matrix m11 = 2 m12 = 3 m21 = 3 m22 = -2 A matrix is like a table with rows (horizontal) andcolumns (vertical).Specific elements are referenced by the pair (row,column).Example: For a general 2 x 2 matrix the math notation is,And the sub-scripted variables take values such as:m11 = 2m12 = 3m21 = 3m22 = -2Example: In Matlab the above matrix is created by>> M = [2 3 ; 3 -2];Note the use of the “ ; “ operator.
5Transpose of a Matrix >> M1 = [2 3 ; 4 5] M1 = 2 3 4 5 >> M2 = M1’M2 =The transpose operator ‘ makes the first row of M1 become the firstcolumn of M2 and the second row of M1 become the second columnof M2.
6Creating a Matrix: Using eye,ones and zeros Arrays can be constructed from built-in Matlab functions (see Pratap pp ).>> A = eye(3) (eye(n) constructs an n x n “identity” matrix)A =1 0 00 1 00 0 1>> A = ones(3,2) (ones(m,n) constructs an m x n array of 1’s) A =1 11 1 >> A = zeros(2,3) (zeros(m,n) constructs an m x n array of 0’s) A =0 0 0
7Creating a Matrix: Using diag Arrays can be constructed from the diag function and a given vector (see 2.9)>> K = [1 2 3];>> diag(K,0)(diag(K,0) constructs a diagonal matrix with values of K down the diagonal)1 0 00 2 0 (green line represents the “diagonal” of a matrix”)0 0 3K = [2 3];>> diag(K,1) (diag(K,1) constructs a matrix with values of K above the diagonal)0 2 00 0 0
8Creating a Matrix: Using diag >> K = [3 4];>> diag(K,-1)(diag(K,-1) constructs a diagonal matrix with values of K below the diagonal)0 0 03 0 00 4 0
9repmat - replicate and tile matrix Arrays can be replicated and tiled to make larger matricies.. Example:>> A = [1 2 ; 3 4]A =3 4Create a matrix named B that tiles A in the following manner>> B = repmat(A,3,4) (constructs an 3 x 4 tiling of A) B =
10Selecting elements from a Matrix - Subscripting Example>> mat1 = [1 -2 ;5 3 ] ;>> mat1(1,1) ( first row ,first column)ans =1 >> mat1(1,2) ( first row ,second column)-2>> mat1(2,1) (second row, first column)5>> mat1(2,2) (second row, second column)3
11Selecting elements from a Matrix - Subscripting You can select an entire row or column of a matrix by using the colon “ : ” symbol.>> mat1 = [ ; ; ] mat1 =>> mat1( 1 , : ) % (first row, any column)ans =>> mat1( : , 2) % (any row , second column)-2310
12Selecting elements from a Matrix - Subscripting >> mat1 = [ ; ; ] mat1 =>> mat1( end , end ) % (third row, fourth column)ans =>> mat1( [1 2] , [1 4] ) % (first and second row, first and fourth column)ans =
13Modifing elements of a Matrix - Subscripting You can use subscripting on a Matlab variable to modifythe value of the matrix. Note that the matrix is locatedon the left hand side of the = .>> mat1 = [1 -2 ; 5 3 ] ;>> mat1(1,:) = 4mat =>> mat1(2, 2) = 7mat1 =>> mat1(: ,1) = [ -1 -2] (or [-1 -2]’)mat1 =
14Arrays - Arithmetic operators * / \ ^.* / \ ^(dot form)add sub mult right div left div powerRules for the valid operations.If A and B are arrays then it is not always true that>>A op Bis defined when op is one of the operations in the table above. The size of an array is its number of rows and columns.We will consider some operators on a case by case basis.
15Arrays - Addition >> A = [2 3 4; 5 6 7] ; >> B = 3; A and B must be of the same size or a scalar.>> A = [2 3 4; 5 6 7] ;>> B = 3;>> A + Bans =5 6 7>> B = [1 2 3; 4 5 6] ;>> A + Bans =3 5 7
16Arrays - Subtraction >> A = [2 3 4; 5 6 7; 8 9 10] ; A and B must be of the same size or a scalar.>> A = [2 3 4; 5 6 7; ] ;>> B = 3;>> A - Bans =-1 0 12 3 4>> B = [1 2 3; 4 5 6; 7 8 9] ;>> A - Bans =1 1 1
17Arrays - (dot) Multiplication The size of A and B must be the same or either could be a scalar .>> A = [2 3 4; 5 6 7] ;>> B = [1 2 3; 4 5 6] ;>> A .* Bans =2 6 12Note: the result is theproduct of the individualelements of the array.For the other operators in the table on slide , try todetermine what the operator does and the sizes for which A op B is valid. We will consider the “ \ “ operator in the future.
18Matrix Product The dot or scalar product of two vectors F * s is: To compute the product “ * ” of two matrices A * B we will take the scalar products of the rows of A with the columns of B.If A has size (m x n) and B has size (n x p) then there are m x p possible combinations.In fact A * B is defined only when the number of columns of A equals the number of rows of B.
19Matrix ProductGiven the two matrices (math notation):
20Matrix Product C(m x p) = A (m x n) * B (n x p) The product of matrix A and B is another matrix C. The size of C is m x p (the number of combinations of scalar products of the rows of A with the columns of B)C(m x p)=A (m x n) * B (n x p)
21Matrix ProductThe possible combinations of scalar products of rows and columns are:( c11 = first row of A * first column of B)( c12 = first row of A * second column of B)
22Matrix ProductThe possible combinations of scalar products of rows and columns are:( c21 = second row of A * first column of B)( c22 = second row of A * second column of B)
23Arrays - Multiplication Example:>> A = [2 3 4; 5 6 7] ;>> B = [1 2 3; 4 5 6; 7 8 9];>> C = A * BC =Note: the size of A * B iscomputed by:size(A) is 2 x 3size(B) is 3 x 3size(A * B) is 2 x 3Note: In generalA * B does not equal B * A.
24Arrays - Multiplication Example:>> A = [2 3 4; 5 6 7] ;>> B = [1 ; 2 ; 3] ;>> C = A * BC =2038Note: again, the size of A * B is computed by:size(A) is 2 x 3size(B) is 3 x 1size(A * B) is 2 x 1
25Linear Systems of Equations Consider the three equations in three unknowns(below):100x + 10y + z = 1225x + 15y + z = 20400x + 20y + z = 10This system of three equations is called linear since the powers on all the variables are one and each variable occurs separately (i.e. no xy or xyz terms).The plot of each equation in 3D is a plane.If the three planes intersect at one point in 3D space, then what is that point?
26Linear Systems of Equations To solve a system of equations A*B = C , i.e. to find the point of intersection of the three planes, use the “ \ ” operator.>> A = [ ; ; ] ;>> C = [1 ; 20 ; 10] ;>> B = A \ CB =You can test this is the solution by typing>>A*B1.0000A is called the“coefficient matrix” for the system of linear equations.The three planes intersect at the point ( , , )
27\ operator - How It Works If we naively wanted to solve the equation for B given A and CA * B = C(and we forgot that A is a matrix, B and C column vectors),then we might try to solve the above for B by dividing bothsides by A.A\(A*B) = A\Cwhere we read the black-slash A\C means A divided into C(A is underneath C). If A\(A*B) = (A\A)*B then since A\A is one we getB = A\CWe can use this heuristic to remember how to use back-slash.The “ \ ” back-slash operator uses a combination of numerical methods including LU decomposition.
28Problem: Weights suspended by springs ceiling-VVVV-K6Problem: Weights suspended by springsW5Hooke’s Law: F = - K*ddi is the length of spring Si,Ki is the i-th spring constant,Wi is the i-th weight,hi is i-th vertical position-VVVV-K5W4unknowns:H-VVVV-K4d1 + d2 + d3 + d4 + d5 +d6 = H,height of floor to ceiling.Also, h1 = d1 and hi = d1+d di for i = 2, ... , 5 .From Newton’s second Law:Ki+1*di+1 - Ki*di = Wi for i=1 , ... ,5 . If we can find the di values then we can compute the hi values.W3K3-VVVV-W2-VVVV-K2W1-VVVV-K1floor
29For a system with five weights K2*d2 - K1*d1 = W1K3*d3 - K2*d2 = W2K4*d4 - K3*d3 = W3K5*d5 - K4*d4 = W4K6*d6 - K5*d5 = W5andd1 + d2 + d3 + d4 + d5 +d6 = HIn math notation:
30Matlab Solution to the Spring-Weight problem Construct the following vectors.K = [K K6];W = [W W5 H] ’ ;Form the matrix A in three steps1) create a “diagonal” matrix>> A = diag(-K,0); A now has the form:
31Matlab Solution to the Spring-Weight problem Form the matrix A in steps2) change the bottom row to a row of 1’s.>> A(end, : ) = 1; A now has the form:
32Matlab Solution to the Spring-Weight problem Form the matrix A in steps3) Put in the values above the diagonal.>> A = A + diag( K(2:end) , 1 ); A now has the form:
33Matlab Solution to the Spring-Weight problem Solve the linear systemA * D = Wby using the back-slash operator “ \ ”>> D = A \ W;and finally, since[h1 , h h3 , h4 , h5 , h6 ] =[d1 ,d1+d2 ,d1+d2+d3 , d1+d2+d3+d4 , ... ]>> H = cumsum(D);
34Function - cumsumThe cumsum function returns the cumulative sum of values for a vector. For a matrix, cumsum operates on the columns of the matrix.Example>> p1 = [1 -2 5]; >> mat1 = [1 -2 ;1 3 ; 2 4] ;>> cumsum(p1)ans =>> cumsum(mat1)
36What have I learned in this lecture? Matlab arrays can be vectors or matrices. You can construct matrices using the semicolon operator “ ; ” which means append values to next row. The functions eye, ones, zeros and diag can also be used to create matrices.You can use subscripting on matrices. Use the colon “ : ” to denote all of the row or column.The back-slash operator “ \ ” is used in obtaining solutions to linear systems of equations.The end command can be used in subscripting to return the last row or column of the matrix (or vector).