R Tutorial : Matrix-Vector Operations

Learn how matrices transform vectors through multiplication, where each output element is the dot product of a matrix row with the vector. The tutorial covers R's %% operator versus element-wise , then applies it to rank sports teams using a Massey matrix built from game outcomes.
Chapters
Data often needs to be transformed into new data for a number of reasons. In this lesson, we'll discuss how matrices can be used to turn vectors into other vectors via multiplication. A matrix with m columns and n rows can only be multiplied by a vector with n elements. The resulting vector then has m elements. Here a, a three by two matrix is being multiplied by a two dimensional vector to make a three dimensional vector. The ith element of a matrix vector multiplication is the element formed by component wise multiplication of the ith row of a given matrix by the given vector and summing the results as shown here. Here the row 1 negative 1 is multiplied by the vector to make the element minus one of the resulting vector. Matrix vector multiplication uses the asterisk symbol sandwiched between 2% symbols in R. The asterisk symbol alone produces what is called component wise multiplication, which is useful in practice, but is not the multiplication we want in this case. Here we have the matrix A multiplied by the vector b, making the vector minus 1,4 0. Here's an example of a 2 by 3 matrix multiplied by a vector with three elements making a vector with two elements. For demonstration's sake, it's good to see what matrix multiplication of A on the left of B is actually doing. For these small examples, notice that each element of A multiplied by B is simply the product of a particular row of A which is a vector and B vector. Vector multiplication in this way is called the dot product. For example, the first element of A times B is the dot product of the first row of A coded with a 1 comma nothing and B, which makes 7. Same for the second element of A times B. An example where matrix vector multiplication is used is the ranking of entities like sports players or teams. And here's an example of a table of outcomes in college football. This is a relatively small example with just 5 teams yielding a matrix with 5 by 5 or 25 elements. In the exercises, you'll deal with a 12 team league which yields a matrix of 144 or 12 times 12 elements. Such a table can be put into a matrix that has the pairwise interactions between all the teams. For example, in this situation, team JH for John Hopkins has played four games, one each against the other four teams. Hence the four in the one, one element of the matrix and A minus one in each of the remaining elements of the row and column in which it resides. RJH is the rating of Johns Hopkins, which has outscored its opponents by 103 points so far. The first element of the vector on the right hand side. We know that who a team plays matters every bit as much as how a team plays. So we want the vector on the left to be an alteration of the vector on the right that reflects the strength of each team given how they played against the teams they played against. This alteration is executed by matrix multiplication by the matrix called the Massey matrix on the left. Now it's your turn.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







