R Tutorial : Matrix-Matrix Calculations

Learn matrix multiplication in R using the %% operator, which produces an M×R matrix from an M×N times N×R matrix. The tutorial covers why order matters (multiplication isn't commutative), identity matrices created with diag(), matrix inverses, and how these tools power neural network weight calculations.
Chapters
Most of the data that data scientists deal with on a regular basis involves not univariate or data with just one case, but rather data with multiple variables. In many cases, these data sets are stored in matrices, and other matrices can be used to alter these data sets for the purpose of prediction. Neural networks are one of the most important algorithms in machine and deep learning, with applications in many areas including computer vision. A key aspect of this algorithm is weighing features of data. This is done using matrix multiplication. A matrix with m rows and n columns, called an M by N matrix, can be multiplied by a matrix with n rows and R columns called an N by R matrix, resulting in a matrix with m rows and R columns called an M by R matrix. Here we see a 2x2 matrix multiplied by another 2 by 2 matrix to make a third 2 by 2 matrix. To perform matrix multiplication, simply multiply the matrix on the left by each of the columns of the matrix on the right using matrix vector multiplication from the previous lecture. Then superimpose the columns remaining to make the third matrix to perform matrix multiplication in R. Again we use the asterisk symbol sandwiched between 2% signs. Two things to note here. First order matters. B times A is not the same thing as A times B. Matrix multiplication is not what's known as commutative. Second, and to reiterate from previously component wise multiplication performed with only the asterisk symbol is different from the traditional matrix multiplication we're learning here. So notice here that a B equals a different matrix than a percent symbol percent symbol B or or B percent symbol *% symbol A. One of the more important elements in the study of linear algebra is the identity matrix. This matrix is such that when multiplied by suitably dimensioned vectors or matrices produces the same vector or matrix back. The identity matrix of dimension N is the n by n matrix created using the diagn command in R. Notice that multiplied with a matrix on both the left and the right, the this matrix produces the original matrix back. Additional concepts that are important when studying matrices are square matrices, a special type of matrix where the number of columns is the same as the number of rows. The matrix inverse, which when multiplied by a suitable matrix yields the identity matrix. In return, singular matrices, which are matrices that don't have an inverse and finally diagonal or triangular matrices which which build many of the matrices we use in practice and are more efficient from a computational perspective. Time to put this into.
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







