Matrix Addition Explained (with Python examples)

This article was first published on PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

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

Table of contents


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

The examples used are fairly simple and don’t require even a calculator. However, the approaches learnt in this article can be applied on more complex matrix additions.

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

Matrix addition explained

Two matrices can be added 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

which can also be simply shown in a matrix:

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

These two farmers have collected some more fruit and are carrying it back to storage. The amount of fruit they collected can also be represented as a table:

CarryingFarmer 1Farmer 2

which can also be simply shown in a matrix:

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

Once brought to the storage, they calculate how much the new total number of fruit in the storage is. And they do it by just adding the fruit number together to get:

TotalFarmer 1Farmer 2

Farmer 1 added apples to apples (3+1=4) and grapes to grapes (7+2=9). Farmer 2 did the same thing, and got 5 (2+3) and 7 (5+2) respectively.

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

$$C = A + B = \begin{bmatrix} 3 & 2 \\ 7 & 5 \end{bmatrix} + \begin{bmatrix} 1 & 3 \\ 2 & 2 \end{bmatrix} = \begin{bmatrix} 3+1 & 2+3 \\ 7+2 & 5+2 \end{bmatrix} = \begin{bmatrix} 4 & 5 \\ 9 & 7 \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} $$

Matrix addition 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 add() function which adds arrays element-wise.

Recall that in Python matrices are constructed as arrays, and that matrices need to be of the same dimension to be added. 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([[1, 3],
              [2, 2]])

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

C = np.add(A, B)


And you should get:

[[4 5]
 [9 7]]

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


In this article we discussed the intuition behind matrix by vector and matrix by matrix multiplication using both linear algebra and graphic approaches, 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.

The post Matrix Addition Explained (with Python examples) appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.