Matrices (Pt. 2): MM

Matrix Multiplication

This is probably where 💩 gets slightly difficult, but it is also where the crux of all data transformation happens 😌. I will firstly walk-through the mechanical operation, and then subsequently a few ways to think about it in the next page.

Matrix-Vector Multiplication Operation

Each row of matrix evaluates with its corresponding column vector

[1234]×[56]=[(1×5)+(2×6)(3×5)+(4×6)]=[1739]\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right] \times \left[\begin{array}{cc} 5 \\ 6 \end{array}\right] = \left[\begin{array}{cc} (1\times5) + (2\times 6)\\ (3\times 5) + (4\times 6) \end{array}\right] = \left[\begin{array}{cc} 17\\ 39 \end{array}\right]

We can break-up the above operation to help assist in understanding.

Step 1:

  • A(1, 1) multiplies with B(1, 1).

  • A(1, 2) multiplies with B(2, 1).

  • The result of C(1, 1) is the summation of elements involved in the interaction from row 1 of Matrix A and col 1 of Matrix B

Step 2:

  • A(2, 1) multiplies with B(1, 1).

  • A(2, 2) multiplies with B(2, 1).

  • The result of C(2, 1) is the summation of elements involved in the interaction from row 2 of Matrix A and col 1 of Matrix B

Test Yourself!

Now, try to perform the following problem before revealing its answer! 👍

[321456789]×[234]=?\left[\begin{array}{cc} 3 & 2 & 1\\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right] \times \left[\begin{array}{cc} 2 \\ 3 \\ 4 \end{array}\right] = ?

Matrix-Matrix Multiplication Operation

Each row of matrix evaluates with each of its respective column vector.

  • Number of columns (n) of left matrix must be equal to number of rows (m) of right matrix

[147258369]×[261369]=[4881779966117]\left[\begin{array}{cc} 1 & 4 & 7\\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array}\right] \times \left[\begin{array}{cc} 2 & 6 \\ 1 & 3 \\ 6 & 9 \end{array}\right] = \left[\begin{array}{cc} 48 & 81 \\ 77 & 99 \\ 66 & 117 \end{array}\right]

Let us break up this problem like we've had previously. 💔

  1. We begin with the first row of the left-matrix, and

  2. Multiply it with the first column of the right-matrix, with

  3. The output element thus occupying the space (1, 1)

Useful Tricks

Size of output

With the previous example laid out, you might question "What would happen if there are not enough elements in my right-matrix to interact with my left?".

With that being a valid question, math simply answers back with a "you can't" 🥁. Hence, the size of both matrices and the non-commutative property (discussed at next page) are important to ensure valid operations.

To recap, we would require

  • The same amount of columns in the left-matrix, to

  • the rows of the right-matrix

For example, a left-matrix of size m x n, has to interact with a right-matrix of size n x o, with m and o being any integers greater than or equals to 1.

Finally, the following might be a trivial, but yet a useful trick to find the size of the output vector.

As it is a requirement for "n" (value 2) to be the same,

  • With the values of "m" (value 2), and

  • "o" (value 1),

  • The size of output variable of m x o (2 x 1) can be seen

The output matrix size is the rows of the left-matrix, by the columns of the right-matrix (m x o).

Standard Method (rows times columns)

To recap from the previous page, an element has the notation of a(i,j),

  • where i is the row number,

  • and j is the column number

Supposed the fact that the above matrix is the output matrix X in the operation (a . b = x),

What then were the factors of A and B used for the computation of a particular element in X?

You may then recall that we previously used the count of row numbers of the left-matrix, and the count of column numbers of the right-matrix to determine the output matrix size.

Similarly, for each output element x(i, j), its factors are the resultant interaction between the

  • "i-th" row of the left matrix, and

  • the "j-th" column of the right-matrix.

This would otherwise be also the standard way to think about matrix multiplication, with:

xi,j=k=1kai,nbn,jx_{\mathrm{i,j}} = \sum_{\mathrm{k=1}}^k a_{\mathrm{i,n}}b_{\mathrm{n,j}}

The following question helps illustrates the discussion.

Let's say we arbitrarily wanted to check the value and the method we obtained x(2, 3).

How do we go about doing so? Easy!

We can do so simply via the 2nd row of the left-matrix, and the 3rd column of the right-matrix.

Columns Method

The output matrix X can be alternatively thought of as

  • A combination of 3 separate columns, from

  • Its corresponding column in right-matrix B,

  • Multiplied by matrix A

Row Method

Alternatively, the output matrix X can be alternatively thought of as

  • A combination of 3 separate rows, from

  • Its corresponding row in left-matrix A,

  • Multiplied by matrix B

More on the next page ⏭

Last updated