Section 2 Matrix Addition
To add matrices the must be the same size. That is, they must have the same number of row and the same number of columns. The operation for matrix addition is done entry wise. That is we will add the same i'th j'th entry from each of the matrices. We can see from this why the matrices need to be the same size. If one of the matrices have entry locations that the other does not, we would not be able to add that entry. Let's consider a general example, and then we will look at an example that is more specific.
Consider matrix,
\(A = \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \end{bmatrix} \) and matrix \(B = \begin{bmatrix} b_{11} & b_{12} & b_{13} & b_{14} \\ b_{21} & b_{22} & b_{23} & b_{24} \\ b_{31} & b_{32} & b_{33} & b_{34} \end{bmatrix} \text{.}\) Then,
This new matrix is the sum of \(A \) and \(B \text{,}\) and we will often rename this matrix something new. For example \(A + B = C \text{.}\)
Next let's take a look at a more specific example.
Example 2.1.
Consider matrix,
\(A = \begin{bmatrix} 1 & 2 & 1 \\ 0 & 5 & -4 \\ 2 & -9 & 1 \end{bmatrix} \) and matrix \(B = \begin{bmatrix} 0 & -2 & 8 \\ 10 & -2 & -4 \\ 7 & -7 & 1 \end{bmatrix} \text{.}\)
Now to find \(A + B \text{,}\) we should sum each of the entries of \(A \) with each of the entires of \(B \text{,}\) entry location by entry location. So,
Next let's take a look at a "non-example", and what the language would look like with that.
Example 2.2.
Consider matrix,
\(A = \begin{bmatrix} 1 & 2 \\ 0 & 5 \\ 2 & -9 \end{bmatrix} \) and matrix \(B = \begin{bmatrix} 0 & -2 & 8 \\ 10 & -2 & -4 \end{bmatrix} \text{.}\)
Since these matrices are not the same size, \(A \) is a \(3 \times 2 \) and \(B \) is a \(2 \times 3 \text{,}\) these matrices are incompatible for matrix addition.
Matrix addition in Python works very similar to as we have written it above. The next block of python code will explore both of the last two example.
Matrix addition in Python for Example 2.1
# Include the numpy module. import numpy as np # Assign matrix A and matrix B from example 2.1 A = np.array([[1, 2, 1], [0, 5, -4], [2, -9 , 1]]) B = np.array([[0,-2, 8], [10, -2, -4], [7, -7 , 1]]) # Compute the sum of A and B and assign it to C C = A + B # Print C (The sum of A and B). print(C)
This next Python example will give us an error. Pay close attention to he wording of this error. It is what we will be looking for if matrices are not compatible for a matrix operation.
Matrix addition in Python for Example 2.2
# Include the numpy module. import numpy as np # Assign matrix A and matrix B from example 2.2 A = np.array([[1, 2], [0, 5], [2, -9 ]]) B = np.array([[0,-2, 8], [10, -2, -4]]) # Compute the sum of A and B and assign it to C C = A + B # Print C (The sum of A and B). print(C)
Here is the value error that we get with the last code-block:
This is a good error to become aware of since it will help us to see when our matrices might be out of shape.