18 August 2023
Summing of Two Matrices in Theory
and Sample Python Application
Let \(A\) and \(B\) be two \(m \times n\) matrices.
$$
A =
\left(
\begin{matrix}
a_{11} & \cdots & a_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1} & \cdots & a_{mn}
\end{matrix}
\right)_{m \times n}
\qquad
B =
\left(
\begin{matrix}
b_{11} & \cdots & b_{1n} \\
\vdots & \ddots & \vdots \\
b_{m1} & \cdots & b_{mn}
\end{matrix}
\right)_{m \times n}
$$
The sum of the two matrices is obtained by adding corresponding entries:
$$
A+B =
\left(
\begin{matrix}
a_{11}+b_{11} & \cdots & a_{1n}+b_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1}+b_{m1} & \cdots & a_{mn}+b_{mn}
\end{matrix}
\right)_{m \times n}
$$
Sample Python Code
import numpy as np
# Create two matrices
A = np.matrix("5 7 3; 4 5 9; 4 2 1")
B = np.matrix("2 9 8; 4 7 3; 5 8 6")
# Add the matrices
result = A + B
print(result)
The Code Represents
$$
A =
\left[
\begin{matrix}
5 & 7 & 3 \\
4 & 5 & 9 \\
4 & 2 & 1
\end{matrix}
\right],
\qquad
B =
\left[
\begin{matrix}
2 & 9 & 8 \\
4 & 7 & 3 \\
5 & 8 & 6
\end{matrix}
\right]
$$
$$
A+B =
\left[
\begin{matrix}
5+2 & 7+9 & 3+8 \\
4+4 & 5+7 & 9+3 \\
4+5 & 2+8 & 1+6
\end{matrix}
\right]
=
\left[
\begin{matrix}
7 & 16 & 11 \\
8 & 12 & 12 \\
9 & 10 & 7
\end{matrix}
\right]
$$