ASSIGNMENT 0 Introduction to Linear Algebra (Basics of vectors and matrices) Due 3:30 PM, Tuesday, October 10 th. Assignments should be submitted via e-mail to: matlabfun.ucsd@gmail.com You can also submit HW0 in person in class. Survey Please go to this link: https://www.surveymonkey.com/r/j25wwcc Completing this survey is required to get full credit for HW0. Homework The goal of HW0 is to review matrices and basic linear algebra. We will play with matrices in MATLAB, but it s important to understand them conceptually before using them in programs. You don t need to worry about terminology per-se or how to implement them in Matlab. That will be covered in class. Definitions for the purposes of this class: Scalar: A single value, (e.g.: -1, 2.35, 10/3) Vector: A series of scalars contained in a column or row e.g. V1 = [ 1, 2, 4.5, 3] row vector 1 V2 = [ 2] column vector 3 Dimensions: How many rows and columns a vector or matrix has. V1 has 1 row and 4 columns or 1x4 V2 has 3 rows and 1 column or 3x1. Note that a vector has either one 1 row or 1 column. Matrix: A series of vectors that form a rectangular shape. Note: Each row or column will have the same amount of values. M1 = [ 1 3 5 ] 2x3 matrix 2 4 6 1 2 M2 = [ 3 4] 3x2 matrix, and transpose of M1 5 6
1 2 3 M3 = [ 4 5 6] 3x3 matrix 7 8 9 Indexing vertices and matrices: You can refer to any part of a matrix or vector with the proper index. Format is ALWAYS (# of row, # of column). And the first row or column is ALWAYS 1 in MATLAB. Students with a programming background may be used to 0 as the first. MATLAB does not work that way. Any one value in a matrix or vector is referred to as an element. M1(2,3) = 6. M2(3,1) = 5 M3(2,1) = 4 V1(1,3) = 4.5 V2(1,2) = DNE (Does not exist because V2 only has one column) Special matrices Identity: A square matrix that when multiplied with another matrix results in the original matrix. Only values are 0 s and 1 s, with 1 s going down diagonally top left to bottom right. 1 0 0 [ 0 1 0] 0 0 1 Transpose: A matrix that has its values flipped across the diagonal. Given an index location (a,b), the value is then swapped with (b,a). Basic operations M3 = [ 2 1 3 4 7 8 ] 2 4 M3 T = M3 = [ 1 7] 3 8 Addition (+) and Subtraction (-) Scalar +/- vector or matrix: Add/Subract the scalar value to each element in a vector or matrix. 3 V1 = 3 [ 1, 2, 4.5, 3] = [2,1, 1.5,6] Vectors and matrices: Add the corresponding elements in the two vectors or two matrices together.
M1 + M3 = [ 1 3 5 2 4 6 ] + [2 1 3 4 7 8 ] = [3 4 8 6 11 14 ] Dimensionality: Matrices and vectors have to have the same dimensions. You cannot add a row vector with a column vector, or a 2x3 matrix with a 3x2 matrix. 1 [ 2] + [1 2 3] = DNE 3 Product Multiplication (*) WARNING: Dimensions of vectors and matrices have to match up properly. When multiplying two vectors and matrices, the number of columns of the first must match the number of rows of the second item. The result has the dimensions of the first item s rows and the second item s columns. (2x3 matrix)*(3x4 matrix) = (2x4 matrix) (1x3 vector)*(3x3 matrix) = (1x3 vector) (1x3 row vector)*(3x1 col vector) = (scalar value or 1x1) Vector Product Multiplication (http://stattrek.com/matrix-algebra/vector-multiplication.aspx): Result is either a scalar (inner product) or matrix (outer product). Go to the link to read up more on vector multiplication. [a b] [ c ] = a c + b d d Note: (1x2 * 2x1 vectors leads to one value. The left side s row is multiplied with each corresponding element in the right sides column. [ a b ] [ a c a d a e c d e] = [ b c b d b e ] Note: (2x1*1x3 vector becomes a 2x3 matrix. The first row on the left only has one element and it is multiplied with one element from each column on the right. Matrix Multiplication (http://stattrek.com/matrix-algebra/matrix-multiplication.aspx): Two matrices always result in another matrix. The dimensionality needs to be confirmed first (inner dimensions must be equal as always). The same method is used as in vector multiplication. a b f g e + b h a f + b i a g + b j [ ] [e ] = [a c d h i j c e + d h c f + d i c g + d j ]
Note: 2x2 * 2x3 = 2x3 matrix. Element (1,1) in the resulting product is the first row elements multiplied and summed with the first column of the second matrix. Element (2,3) is the second row of the first matrix ([c,d]) times the third column of the second matrix ([g;j], etc. Element-wise multiplication (.*) Multiply the same element in each matrix or vector with a given value. Matrices and vectors have to be the same dimension! a b c h i g b h c i [ ]. [g ] = [a d e f j k l d j e k f l ] Note: In MATLAB, element-wise multiplication is denoted with a period followed by multiplication. You can also do division like this as well. Addition and subtraction are already element-wise operations. This covers the majority of the linear algebra concepts you will need for this class. The links above go into more detail, and any other questions can be addressed to the TA or professor. Questions Addressing array elements 1) What is V(5) in row vector V = [ 1 0 2 4 6]? 1 2 2) What is M(2,3) in the matrix M = [ 9 8]? 0 5 3) What is M(3,2)? Matrix operations Underline/Box/Circle true or false for the following questions. 4) True or False? When adding two matrices, the dimensions of the two matrices must be equal. 5) True or False? When multiplying two matrices, the dimensions of the two matrices must be equal.
6) True or False? The product matrix A and matrix B always has dimensions equal to either A or B. 7) True or False? You can only multiply a scalar and a square matrix. Matrices: A,B,C, and D and Scalars: s and t are used in the following exercises. 1 1 9 3 2 1 0 1 1 1 0 0 2 8 A = [ 5 5 6 33] B = [ ] C = [ ] D = [ 3 3 0 3] s = 2 t = 0.5 1 3 7 9 8 7 0.2 0 2 2 2 0 4 6 Compute the answers to the following expressions or indicate that there is no answer (DNE). + means addition, * means multiplication, * means element-by-element multiplication, means transpose of the matrix or vector. 8) A+s 9) B-s 10) D*t 11) A+D 12) A+B 13) A*C
14) A.*D 15) D*B 16) D*A 17) B.*B 1 2.3 4 1 0 0 18) [ 5 6 1.2 ] [ 0 1 0] = 3.5 5.6 9.4 0 0 1