In this article we will discuss the steps and intuition for matrix addition with examples and perform matrix subtraction in Python.

Table of contents


Introduction

In this article we explain the intuition and steps of matrix subtraction.

The examples used are fairly simple and don’t require even a calculator. The logic is very similar to the process of matrix additions as well. However, the approaches learnt in this article can be applied on more complex matrix subtractions.

We also explore how quick and easy it is to perform matrix addition using Python.

To continue following this tutorial we will need the following Python library: numpy.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:


pip install numpy

Books I recommend:

[the_ad id=”2889″]
[the_ad id=”2891″]
[the_ad id=”2892″]
[the_ad id=’2894′]

Matrix subtraction explained

A matrix can be subtracted from another matrix if and only if they have the same dimension (both are 2×2, 3×3, and so on).

For an intuitive example let’s consider 2 farmers who both have some apples and some grapes in storage. We can represent this as a table:

StorageFarmer 1Farmer 2
Apples32
Grapes75

which can also be simply shown in a matrix:

$$A = \begin{bmatrix} 3 & 2 \\ 7 & 5 \end{bmatrix}$$


These two farmers then went to the market and sold some fruit. The amount of fruit they sold can also be represented as a table:

SoldFarmer 1Farmer 2
Apples21
Grapes13

which can also be simply shown in a matrix:

$$B = \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix}$$


Once the market closes, they calculate how much the new total number of fruit in the storage is after they sold some of it. And they do it by just subtracting the sold fruit number to get:

TotalFarmer 1Farmer 2
Apples11
Grapes62

Farmer 1 subtracted apples from apples (2-1=1) and grapes from grapes (7-1=6). Farmer 2 did the same thing, and got 1 (2-1) and 2 (5-3) respectively.

Now let’s do the same thing in matrix form:

$$C = A – B = \begin{bmatrix} 3 & 2 \\ 7 & 5 \end{bmatrix} – \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix} = \begin{bmatrix} 3-2 & 2-1 \\ 7-1 & 5-3 \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 6 & 2 \end{bmatrix}$$

And we see that matrix \(C\) has the same values as the table with totals above it.


We can further generalize this approach for a \(m \times n\) dimension matrix:

$$A – B = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix} – \begin{bmatrix} b_{11} & b_{12} & \dots & b_{1n} \\ b_{21} & b_{22} & \dots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \dots & b_{mn} \end{bmatrix} = \\[50pt] = \begin{bmatrix} a_{11}-b_{11} & a_{12}-b_{12} & \dots & a_{1n}-b_{1n} \\ a_{21}-b_{21} & a_{22}-b_{22} & \dots & a_{2n}-b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}-b_{m1} & a_{m2}-b_{m2} & \dots & a_{mn}-b_{mn} \end{bmatrix}$$

[the_ad id=”3031″]

Matrix subtraction in Python

In order to perform the matrix vector multiplication in Python we will use the numpy library. And the first step will be to import it:


import numpy as np

Numpy has a lot of useful functions, and for this operation we will use the subtract() function which subtracts arrays element-wise.

Recall that in Python matrices are constructed as arrays, and that matrices need to be of the same dimension to be subtracted. And the next step will be to define the input matrices.

We are going to use the same 2×2 matrices as in the previous section:


A = np.array([[3, 2],
              [7, 5]])

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

Now that we have the required matrices, we can easily calculate the matrix resulting from the matrix subtraction:


C = np.subtract(A, B)

print(C)

And you should get:

[[1 1]
 [6 2]]

which is exactly the same output as in our example where we calculated it manually.

[the_ad id=”3031″]

Conclusion

In this article we discussed the intuition and steps for matrix subtraction, as well as shown complete examples using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.