# Math: How to Do a Matrix Multiplication

*I studied applied mathematics, in which I did both a bachelor's and a master's degree.*

## What Is a Matrix?

A matrix is an array of numbers that is rectangular. It can be used to do linear operations such as rotations, or it can represent systems of linear inequalities.

A matrix is generally denoted with the letter *A*, and its dimensions by *n* and *m*. This means a matrix has *n* rows and *m* columns and therefore *n*m* entries.

### Example

A linear system can be written down with the use of a matrix. Let's look at the following system:

*a + b + c = 3*

*2a + 3c = 4*

*5b - 2c = 7*

This can be written down as a matrix times a vector equals a vector. This is shown in the picture below.

This gives a much clearer view of the system. In this case, the systems consists of only three equations. Therefore, the difference is not so big. However, when the system has many more equations, the matrix notation becomes the preferred one. Furthermore, there are many properties of matrices that can help in solving these kinds of systems.

## Matrix Multiplication

Multiplying two matrices is only possible when the matrices have the right dimensions. An *m* times *n* matrix has to be multiplied with an *n* times *p* matrix. The reason for this is because when you multiply two matrices you have to take the inner product of every row of the first matrix with every column of the second.

This can only be done when both have the same length. The result of the multiplication will be an *m* times *p* matrix. So it does not matter how many rows *A* has and how many columns *B* has, but the length of the rows of *A* must be equal to the length of the columns of *B*.

Multiplying two numbers can be seen as a matrix multiplication between two 1x1 matrices. In this case, *m, n *and *p *are all equal to 1. Therefore we are allowed to perform the multiplication.

When multiplying two matrices,* A* and *B,* we can determine the entries of this multiplication as follows:

When *A*B = C* we can determine entry *c_i,j* by taking the inner product of the* i'th* row of *A* with the* j'th* column of *B*.

### Inner Product

The inner product of two vectors *v* and *w* is equal to the sum of* v_i*w_i* for* i* from 1 to *n*. Here *n* is the length of the vectors *v* and *w*. An example:

*(2,4,3) • (1,5,7) = 2*1 + 4*5 + 3*7 = 43*

Another way to define the inner product of *v* and *w* is to describe it as the product of *v* with the transpose of *w*. An inner product is always a number. It can never be a vector.

The following picture gives a better understanding of exactly how matrix multiplication works.

In the picture we see that *1*7 + 2*9 + 3* 11 = 58* forms the first entry. The second is determined by taking the inner product of *(1,2,3)* and* (8,10,12),* which is *1*8+3*10+3*12 = 64.* Then the second row will be *4*7 + 5*9 + 6* 11 = 139* and *4*8 + 5*10 + 6*12 = 154.*

As you can see a 2-times-3 matrix multiplied by a 3-times-2 matrix gives a 2-times-2 square matrix.

## Properties of Matrix Multiplication

Matrix multiplication does not have the same properties as normal multiplication. First, we don't have commutativity, which means that *A*B* does not have to be equal to *B*A*. This is a general statement. This means that there are matrices for which *A*B = B*A, *for example when *A* and *B *are just numbers. However, it is not true for any pair of matrices.

It does, however, satisfy associativity, which means *A*(B*C) = (A*B)*C*.

It also satisfies distributivity, meaning *A(B+C) = AB + AC*. This is called left distributivity.

Right distributivity means *(B+C)A = BA + CA*. This is also satisfied. Note, however, that *AB + AC* is not necessarily equal to *BA + CA* since matrix multiplication is not commutative.

### Special Kinds of Matrices

The first special matrix that comes up is a **diagonal matrix**. A diagonal matrix is a matrix which has non-zero elements on the diagonal and zero everywhere else. A special diagonal matrix is the **identity matrix**, mostly denoted as* I*. This is a diagonal matrix where all diagonal elements are 1. Multiplying any matrix *A* with the identity matrix, either left or right results in *A*, so:

*A*I = I*A = A*

Another special matrix is the **inverse matrix** of a matrix *A*, mostly denoted as *A^-1.* The special property here is as follows:

*A*A^-1 = A^-1*A = I*

So multiplying a matrix with its inverse results in the identity matrix.

Not all matrices have an inverse. First of all, a matrix needs to be square to have an inverse, but even that is not enough. A square matrix that does not have an inverse is called a singular matrix, and therefore a matrix that does have an inverse is called non-singular.

A matrix has an inverse if and only if its determinant is not equal to zero. So any matrix that has a determinant equal to zero is singular, and any square matrix that doesn't have a determinant equal to zero has an inverse.

### Different Kinds of Matrix Multiplication

The way described above is the standard way of multiplying matrices. There are some other ways to do it that can be valuable for certain applications. Examples of these different multiplication methods are the Hadamard product and the Kronecker product.

## Summary

Two matrices *A* and *B* can be multiplied if the rows of the first matrix have the same length as the columns of the second matrix. Then the entries of the product can be determined by taking the inner products of the rows of *A* and the columns of *B*. Therefore *AB* is not the same as *BA*.

The identity matrix *I* is special in the sense that *IA = AI = A*. When a matrix *A* is multiplied with its inverse* A^-1* you get the identity matrix *I*.

*This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.*