MATHEMATICS FOR AI • LESSON 2

Matrix Multiplication

Matrix multiplication is a way to combine two matrices to produce a new matrix. It is one of the most important operations in linear algebra and is used heavily inside machine learning and neural networks.

CORE IDEA

Matrix multiplication combines rows from one matrix with columns from another.

To calculate one value in the result, we multiply the numbers from one row by the corresponding numbers from one column and then add the results together.

01

What Is Matrix Multiplication?

Matrix multiplication means multiplying two matrices together to create another matrix.

The important point is that matrix multiplication is not simply multiplying every number with every other number.

Instead, we use a specific rule: row × column.

Matrix A

[ 1  2 ]
[ 3  4 ]


Matrix B

[ 5  6 ]
[ 7  8 ]

We multiply matrix A by matrix B:

A × B

[ 1  2 ]   [ 5  6 ]
[ 3  4 ] × [ 7  8 ]
02

The Row × Column Rule

This is the most important rule to understand.

To calculate one value in the result, take one row from the first matrix and one column from the second matrix.

Row from A

[ 1  2 ]


Column from B

[ 5 ]
[ 7 ]

Multiply the corresponding numbers:

(1 × 5) + (2 × 7)

= 5 + 14

= 19

Therefore, the first value of the result is 19.

03

Calculating the Complete Result

Now let's calculate every value in the resulting matrix.

We already calculated the first value:

First value

(1 × 5) + (2 × 7)
= 19

Now calculate the second value using the first row of A and the second column of B:

(1 × 6) + (2 × 8)

= 6 + 16

= 22

Now use the second row of A and the first column of B:

(3 × 5) + (4 × 7)

= 15 + 28

= 43

Finally, use the second row of A and the second column of B:

(3 × 6) + (4 × 8)

= 18 + 32

= 50

Therefore, the final result is:

[ 1  2 ]   [ 5  6 ]   [ 19  22 ]
[ 3  4 ] × [ 7  8 ] = [ 43  50 ]
04

Why Does Row × Column Work?

Think of each value in the result as the combination of one row and one column.

        Matrix B

          Column 1     Column 2
             ↓            ↓

A Row 1 →  [ 5  6 ]
A Row 2 →  [ 7  8 ]

The first result value comes from:

Row 1 of A × Column 1 of B

[ 1  2 ] × [ 5 ]
           [ 7 ]

= (1 × 5) + (2 × 7)

= 19

The second result value comes from:

Row 1 of A × Column 2 of B

[ 1  2 ] × [ 6 ]
           [ 8 ]

= (1 × 6) + (2 × 8)

= 22

The same process is repeated for every position in the result matrix.

05

Matrix Dimensions Must Match

Matrix multiplication has an important rule about dimensions.

If the first matrix has dimensions m × n, the second matrix must have dimensions n × p.

A = 2 × 3

B = 3 × 2

A × B is valid.

The inside numbers must be the same:

2 × 3
    ×
3 × 2

      ↑
These numbers match

The resulting matrix will have the outside dimensions:

2 × 3

    ×

3 × 2

↓

2 × 2

So:

(2 × 3) × (3 × 2) = 2 × 2
06

A Simple Dimension Example

Consider these two matrices:

A = 2 × 3

[ 1  2  3 ]
[ 4  5  6 ]


B = 3 × 2

[ 7   8 ]
[ 9  10 ]
[11  12 ]

The multiplication is valid because:

2 × 3

×

3 × 2

↓

2 × 2

The result will therefore contain 2 rows and 2 columns.

07

Matrix Multiplication in Python

In Python, NumPy provides a simple way to perform matrix multiplication.

import numpy as np

A = np.array([
    [1, 2],
    [3, 4]
])

B = np.array([
    [5, 6],
    [7, 8]
])

result = A @ B

print(result)

The output is:

[[19 22]
 [43 50]]

The @ operator performs matrix multiplication in Python.

08

Matrix Multiplication in AI

Matrix multiplication is not just a mathematical exercise. It is one of the core operations used inside machine learning models.

Neural networks use matrices to represent things such as input data, weights, and transformations.

Input Data
     ↓
Matrix
     ↓
Matrix Multiplication
     ↓
Weights
     ↓
New Values
     ↓
Next Layer

A neural network can perform many matrix multiplications while transforming input data into a prediction.

09

Matrix Multiplication vs Element-by-Element Multiplication

These two operations are different. This is a common beginner mistake.

Matrix multiplication uses the row × column rule.

Element-by-element multiplication simply multiplies numbers in the same positions.

A = [ 1  2 ]
    [ 3  4 ]

B = [ 5  6 ]
    [ 7  8 ]

Matrix multiplication:

A × B

= [ 19  22 ]
  [ 43  50 ]

Element-by-element multiplication:

[1×5   2×6]
[3×7   4×8]

= [ 5   12 ]
  [21   32 ]

They produce completely different results.

10

The Main Idea to Remember

You do not need to memorize a complicated formula first. Understand the process.

Take a row
    ↓
Take a column
    ↓
Multiply corresponding values
    ↓
Add the products
    ↓
Get one result value
    ↓
Repeat for every position

Once this becomes familiar, matrix multiplication becomes much easier to understand.

WHAT TO REMEMBER

Matrix multiplication is based on the row × column rule.

To calculate each value in the result, take a row from the first matrix and a column from the second matrix. Multiply the corresponding values and add them together. Also remember that the inner dimensions must match before two matrices can be multiplied.