import numpy as np

We can use the matrix function in the numpy module to create a matrix. When creating a matrix, each row is separating by ;. So for matrix A below, the first row is 1 3 4 5 and the second row is 3 5 7 8. Run the code chunk below to see this matrix.

A = np.matrix('1 3 4 5; 3 5 7 8')
A
## matrix([[1, 3, 4, 5],
##         [3, 5, 7, 8]])

If we want to create a matrix with three rows, you separate each of the rows by ;. You can see this with matrix B below. Run the code chunk to see matrix B.

B = np.matrix('1 2 3; 4 5 6; 7 8 9')
B
## matrix([[1, 2, 3],
##         [4, 5, 6],
##         [7, 8, 9]])

In Python, the first row of a matrix is indicated by 0, the second row is indicated by 1, and so on. In the code chunk below, we are looking at the first row of matrix B.

B[0] 
## matrix([[1, 2, 3]])

Note: In R, putting a number in between two brackets without a comma indicates an element of the matrix. In Python, a number in between two brackets without a comma indicates a specific row.

If you uncomment the code chunk below, you will see that there is an error. Although we have three rows in matrix B, 3 corresponds to the 4th row. Matrix B does not have a 4th row, therefore python cannot print it.

#B[3]

Like the rowSum() function in R, we can find the sum of a row in Python. We do this a litte differely though. We indicate which row of the matrix that we want to look at then we use the sum function.

B[0].sum()
## 6

We also can take the sum of a whole matrix. Run the code chunk below to see this.

B.sum()
## 45

Similar to R, we can do arithmatic operations to matrices. Run the two code chunks below to see how this works.

A + 5
## matrix([[ 6,  8,  9, 10],
##         [ 8, 10, 12, 13]])
A - 3
## matrix([[-2,  0,  1,  2],
##         [ 0,  2,  4,  5]])
A * 2
## matrix([[ 2,  6,  8, 10],
##         [ 6, 10, 14, 16]])
A / 4
## matrix([[0.25, 0.75, 1.  , 1.25],
##         [0.75, 1.25, 1.75, 2.  ]])
B + 5
## matrix([[ 6,  7,  8],
##         [ 9, 10, 11],
##         [12, 13, 14]])
B * 2
## matrix([[ 2,  4,  6],
##         [ 8, 10, 12],
##         [14, 16, 18]])
B - 3
## matrix([[-2, -1,  0],
##         [ 1,  2,  3],
##         [ 4,  5,  6]])
B / 4
## matrix([[0.25, 0.5 , 0.75],
##         [1.  , 1.25, 1.5 ],
##         [1.75, 2.  , 2.25]])

Let’s introduce a matrix C. Notice how matrix C has the same number of rows and columns as matrix A.

C = np.matrix('5 3 1 4; 4 9 2 8')
C
## matrix([[5, 3, 1, 4],
##         [4, 9, 2, 8]])

Since matrix C is the same size as matrix A, we can add them to and subtract them from each other. Run the next two code chunks to see this.

A + C
## matrix([[ 6,  6,  5,  9],
##         [ 7, 14,  9, 16]])
A - C
## matrix([[-4,  0,  3,  1],
##         [-1, -4,  5,  0]])

If you uncomment the code chunk below and try to run it, you will get an error. Why do you think this is?

#A + B

Similar to R, we can add and subtract matrices with vectors. As long as the number of elements in a vector is the same as the number of columns in a matrix, you will not get an error.

In the code chunk below we are creating a vector x. Vector x has 4 elements, so we will add it to matrix A because matrix A has 4 columns.

x = np.array([1, 2, 3, 4])
A = np.matrix('1 3 4 5; 3 5 7 8')
x + A
## matrix([[ 2,  5,  7,  9],
##         [ 4,  7, 10, 12]])

Examples

Create a matrix with the first row containing numbers 1 through 4 and the second row containing numbers 5 through 8. Name this matrix J.

J = np.matrix('1 2 3 4; 5 6 7 8')
J
## matrix([[1, 2, 3, 4],
##         [5, 6, 7, 8]])

Add 6 to the matrix J.

J + 6
## matrix([[ 7,  8,  9, 10],
##         [11, 12, 13, 14]])

Divide matrix J by 3.

J / 3
## matrix([[0.33333333, 0.66666667, 1.        , 1.33333333],
##         [1.66666667, 2.        , 2.33333333, 2.66666667]])

Print the second row of matrix J.

J[1]
## matrix([[5, 6, 7, 8]])

Create a matrix R with the same number of rows and columns as J.

R = np.matrix('2 3 4 5; 6 7 8 9')
R
## matrix([[2, 3, 4, 5],
##         [6, 7, 8, 9]])

Add matrix R and matrix J.

R + J
## matrix([[ 3,  5,  7,  9],
##         [11, 13, 15, 17]])

Subtract matrix R from matrix J.

J - R
## matrix([[-1, -1, -1, -1],
##         [-1, -1, -1, -1]])

Practice

1.) Create a matrix with the first row containing numbers 2 through 7 and the second row containing numbers 8 through 13. Name this matrix W.

2.) Add 3 to matrix W.

3.) Multiple matrix W by 9.

4.) Print the first row of W.

5.) Create a matrix N with the same number of rows and columns as W.

6.) Add matrix N and matrix W.