<<

REVIEW OF MATRICES AND BASIC LINEAR

We will play with matrices in MATLAB, but it’s important to understand them conceptually also. You don’t need to worry about terminology per se or how to implement them in Matlab as it will be covered in class.

Definitions for the purposes of this class:

Scalar: A numerical value

Vector: A series of scalars contained in a column or row

e.g. �1 = [ 1 2 41 3] row vector

1 �2 = 2 column vector 3

Dimensions: How many rows and columns a vector or 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.

1 3 5 �1 = 2x3 matrix 2 4 6 1 2 �2 = 3 4 3x2 matrix, and of M1 5 6 1 2 3 �3 = 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 rows, # of columns). And the first row or column is 1 in MATLAB. (Students with a programming background may be used to 0 as the first in some languages.) 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) = 41 V2(1,2) = DNE (Does Not Exist - because V2 only has one column. In MATLAB you will get an index error)

Identity matrix: A that when multiplied with another matrix results in the original matrix. It has only the values 0’s and 1’s, arranged as 1’s going down diagonally top left to bottom right and 0 elsehwhere. Example: The of size 3 (or 3x3) is:

1 0 0 0 1 0 0 0 1

Matlab allows you to create identity matrices using the function eye. See also: zeros and ones.

Matrix Operations

Addition (+) and Subtraction (-)

Scalar +/- vector or matrix: Add/Subtract the scalar value to each element in a vector or matrix.

3 − �1 = 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.

1 3 5 2 1 3 3 4 8 �1 + �3 = + = 2 4 6 4 7 8 6 11 14

Dimensionality: Matrices and vectors have to have the same dimensions for addition to work. So you cannot add a row vector with a column vector, or a 2x3 matrix with a 3x2 matrix.

Transpose: This operation (‘) yields a matrix that has its values flipped across the diagonal. Given an index location (a,b), the value is then swapped with (b,a) in the transposed matrix.

2 1 3 �3 = 4 7 8 2 4 �3 = 1 7 3 8

Matrix (inner )

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 (that is, the inner dimensions must match). 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)

Vectors: Result is either a scalar (inner product) or matrix (). Go to the link to read up more on vector multiplication. (http://stattrek.com/matrix-algebra/vector- multiplication.aspx): � [ ] ∗ = � ∗ � + � ∗ � � � � Note: (1x2 * 2x1 vectors leads to one value. The left side’s row is multiplied with each corresponding element in the right sides’ column.

� � ∗ � � ∗ � � ∗ � ∗ [� � �] = � � ∗ � � ∗ � � ∗ �

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.

Matrices: 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. (http://stattrek.com/matrix-algebra/matrix-multiplication.aspx):

� � � � � � ∗ � + � ∗ ℎ � ∗ � + � ∗ � � ∗ � + � ∗ � ∗ = � � ℎ � � � ∗ � + � ∗ ℎ � ∗ � + � ∗ � � ∗ � + � ∗ �

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 (or .*)

Multiply the same element in each matrix or vector with a given value, like in addition. Matrices and vectors have to be the same dimension.

� � � � ℎ � � ∗ � � ∗ ℎ � ∗ � .∗ = � � � � � � � ∗ � � ∗ � � ∗ �

Note: In MATLAB, element-wise multiplication is denoted with a period followed by multiplication. You can also do like this as well. Addition and subtraction are already element-wise operations.

This covers the concepts you will need for this class. The links above go into more detail, and any other questions can be addressed to the TA/IA or professor.