HOME Mathematics Computer Science
Matrix Addition
Prerequisites
Show All
Hide All
Matrices
Show/Hide

Definition
A matrix is a rectangular group of numbers (or symbols) with a given number of rows and columns.
Example

The following matrix has two rows and three columns, and is described as a "two by three" (2x3) matrix.

LaTeX
\begin{bmatrix}
    3 & 6 & 2  \\
    5 & 1 & 10
\end{bmatrix}
Notation
  • There are a few different ways to represent a matrix. The easiest is with an bold uppercase letter: A
Code
A = [[3, 6, 2], [5, 1, 10]]

print(A)
Notes
  • The plural of matrix is matrices.
  • The size of a matrix is called its dimension or order. It is written with as rows x columns. A matrix with 2 rows and 3 columns is written as 2x3.
  • Two matrices are considered equal if and only if:
    • They have the same number of rows.
    • They have the same number of columns.
    • Each corresponding element is equal. i.e. x111 = x211
  • The numpy.matrix is depreciated, in favour of regular arrays
Elements In A Matrix
Show/Hide

Introduction
  • Each element in a matrix can be uniquely referred to by its position.
  • Each element is appended a subscript containing its row position and column position respectively.
    • For example: x21 refers to the element in row 2, column 1
  • The index of the row or column always starts at 1 (as opposed to programming where it starts at 0).
Example
LaTeX
\begin{bmatrix}
    x_{11}       & x_{12} & x_{13} & \dots & x_{1n} \\
    x_{21}       & x_{22} & x_{23} & \dots & x_{2n} \\
    \hdotsfor{5} \\
    x_{d1}       & x_{d2} & x_{d3} & \dots & x_{dn}
\end{bmatrix}
Code

In the below code we access element x21, however we refer to it as a[1, 0] because in programming indexes start at 0, not 1.

import numpy as np

a = np.matrix([[3, 6, 2], [5, 1, 10]])

print(a)  # prints [[ 3  6  2]
          #         [ 5  1 10]]


print(a[1, 0])  # prints '5'

a[1, 0] = 3
print(a[1, 0])  # prints '3'

Matrix Addition

Introduction
  • Matrices can be added and subtracted to produce new matrices.
  • This is only possible if the order (dimension) of the matrices is the same.
  • To add (or subtract) two matrices, you add (or subtract) the corresponding elements.
Mathematical Definition
<
Example
Code
import numpy as np

a = [[3, 6, 2], [5, 1, 10]]
b = [[7, 8, 1], [1, 5, 2]]

c = np.add(a, b)
print(c)