Knowledge Base

Matrices and Matrix Operations

Creating matrices

A matrix is a rectangular numeric table or two-dimensional array. It consists of mm rows and nn columns (the size is written as 𝑚×𝑛𝑚×𝑛). Matrices are usually denoted by uppercase Latin letters, and their elements are lowercase with a double index: aija_{ij}, where ii is the row number and jj is the column number.

A=(a11a12a13a21a22a23)=(123234)\begin{aligned}A &= \begin{pmatrix} a_{11} & a_{12} & a_{13 }\\ a_{21} & a_{22} & a_{23} \end{pmatrix} \\&= \quad \begin{pmatrix} 1 & 2 & 3 \\ 2 & 3 & 4\end{pmatrix}\end{aligned}

Call np.array() to create a numpy matrix from a list of lists. All nested lists are the same length.

1import numpy as np
2
3matrix = np.array([
4 [1, 2, 3],
5 [4, 5, 6],
6 [7, 8, 9]])
7print(matrix)

Let's take a list of vectors instead of a list of lists:

1import numpy as np
2
3string0 = np.array([1,2,3])
4string1 = np.array([-1,-2,-3])
5list_of_vectors = [string0, string1]
6matrix_from_vectors = np.array(list_of_vectors)
7
8print(matrix_from_vectors)

Create a matrix from the pandas table: its values attribute is a matrix.

1import pandas as pd
2import numpy as np
3
4matrix = df.values
5print(matrix)

The shape attribute defines the size of the matrix 𝐴𝐴. Its 𝑎𝑖𝑗𝑎_{𝑖𝑗} element is set in numpy as A[i,j]: rows and columns are numbered from zero, just like array indices.

1import numpy as np
2
3A = np.array([
4 [1, 2, 3],
5 [2, 3, 4]])
6
7print('Size:', A.shape)
8print('A[1, 2]:', A[1, 2])

Select individual rows and columns from the matrix:

1import numpy as np
2
3matrix = np.array([
4 [1, 2, 3],
5 [4, 5, 6],
6 [7, 8, 9],
7 [10,11,12]])
8
9print('Row 0:', matrix[0, :])
10print('Column 2:', matrix[:, 2])

Operations with matrix elements

You can do the same operations with matrix elements as you do with vector elements. Two matrices can be added, subtracted, multiplied, or divided. The most important part is that the operations are performed element-by-element, and the matrices are the same size. The result of an operation is a matrix of the same size.

1import numpy as np
2
3matrix1 = np.array([
4 [1, 2],
5 [3, 4]])
6
7matrix2 = np.array([
8 [5, 6],
9 [7, 8]])
10
11print(matrix1 + matrix2)

You can multiply a matrix by a number, add a number, or subtract it: the operation is applied to each element.

1import numpy as np
2
3matrix = np.array([
4 [1, 2],
5 [3, 4]])
6
7print(matrix * 2)
8print(matrix - 2)

Multiplying a matrix by a vector

Each row of this list (matrix) is a vector multiplied by a scalar, and the resulting numbers form a new vector.

For example, a matrix 𝐴𝐴 with the 𝑚×𝑛𝑚×𝑛 size is multiplied by a vector bb (nn-dimensional). The product will be a new vector c=𝐴bc=𝐴b. This is a 𝑚𝑚-dimensional vector whose ii coordinate is equal to the scalar product of row ii of the matrix and bb.

(a11a12a1na21a22a2nam1am2amn)(b1b2bn)=(c1c2cn)\begin{pmatrix} \color{teal}a_{11} & \color{teal}a_{12} & \color{teal}\dots & \color{teal}a_{1n} \\ \color{maroon}a_{21} & \color{maroon}a_{22} & \color{maroon}\dots & \color{maroon}a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ \color{cornflowerblue}a_{m1} & \color{cornflowerblue}a_{m2} & \color{cornflowerblue}\dots & \color{cornflowerblue}a_{mn} \\ \end{pmatrix} \cdot \begin{pmatrix} \color{#a38b00}b_1 \\ \color{#a38b00}b_2 \\ \color{#a38b00}\vdots \\ \color{#a38b00}b_n \end{pmatrix} = \begin{pmatrix} c_1 \\ c_2 \\ \vdots \\ c_n \end{pmatrix}
c1=(a11b1)+(a12b2)++(a1nbn)c2=(a21b1)+(a22b2)++(a2nbn)cm=(am1b1)+(am2b2)++(amnbn)\begin{aligned}c_1 &= (\color{teal}a_{11}\color{default}* \color{#a38b00}{b_1}\color{default} ) +(\color{teal}a_{12}\color{default}*\color{#a38b00}{b_2}\color{default})+\dots+(\color{teal}a_{1n}\color{default}*\color{#a38b00}{b_n}\color{default}) \\ c_2 &= (\color{maroon}a_{21}\color{default}*\color{#a38b00}{b_1}\color{default})+(\color{maroon}a_{22}\color{default}*\color{#a38b00}{b_2}\color{default})+\dots+(\color{maroon}a_{2n}\color{default}*\color{#a38b00}{b_n}\color{default}) \\ &\qquad\qquad\qquad\qquad\vdots \\c_m &= (\color{cornflowerblue}a_{m1}\color{default}*\color{#a38b00}{b_1}\color{default})+(\color{cornflowerblue}a_{m2}\color{default}*\color{#a38b00}{b_2}\color{default})+\dots+(\color{cornflowerblue}a_{mn}\color{default}*\color{#a38b00}{b_n}\color{default})\end{aligned}

Let's perform this operation in numpy and call the familiar to us the np.dot() function.

1import numpy as np
2
3A = np.array([
4 [1, 2, 3],
5 [4, 5, 6]])
6
7b = np.array([7, 8, 9])
8
9print(np.dot(A, b))
10print(A.dot(b))

For multiplication to be correct, the size of the vector must be equal to the width of the matrix.

Transpose of matrix

The transpose of a matrix is its "flip" over the main diagonal of the matrix, which goes from the upper-left to the lower-right corner. With this reversal, the matrix AA of the 𝑚×𝑛𝑚×𝑛 size is transformed into a matrix of the 𝑛×𝑚𝑛×𝑚 size. In other words, the rows of the matrix become its columns, and the columns become its rows. The transposed matrix is indicated by the upper index T.

(a11a12a1na21a22a2nam1am2amn)=(a11a21am1a12a22am2a1na2nanm)\begin{pmatrix} \color {red} a_{11} & \color {red}a_{12} & \color {red}\dots & \color {red}a_{1n} \\ \color {green}a_{21} & \color {green}a_{22} & \color {green}\dots & \color {green}a_{2n} \\ \color {olive}\vdots &\color {olive} \vdots & \color {olive}\ddots &\color {olive} \vdots \\ \color {orange}a_{m1} & \color {orange}a_{m2} & \color {orange}\dots & \color {orange}a_{mn} \\ \end{pmatrix}^\top = \begin{pmatrix} \color {red} a_{11} & \color {green}a_{21} & \color {olive}\dots & \color {orange}a_{m1} \\ \color {red}a_{12} & \color {green}a_{22} & \color {olive}\dots & \color {orange}a_{m2} \\ \color {red}\vdots &\color {green} \vdots & \color {olive}\ddots &\color {orange} \vdots \\ \color {red}a_{1n} & \color {green}a_{2n} & \color {olive}\dots & \color {orange}a_{nm} \\ \end{pmatrix}

In NumPy, this operation is set by the T attribute. If you need to build a matrix, start from the list of columns to create a matrix and apply transpose:

1import numpy as np
2
3matrix = np.array([
4 [1, 2],
5 [4, -4],
6 [0, 17]])
7
8print("Transposed matrix")
9print(matrix.T)

Multiply the original matrix by a vector with the length equal to 3:

1vector = [2, 1, -1]
2print(np.dot(matrix, vector))

We got an error: the dimensions of the matrix (3, 2) and the vector (3,) are not aligned. The second dimension of the matrix is not equal to the length of the vector. To make the multiplication correct, we transpose the matrix:

1print(np.dot(matrix.T, vector))

Matrix multiplication

During Matrix multiplication, a third matrix is constructed using two matrices. It consists of scalar products of the rows of the first matrix by the columns of the second. The product of the ii row of the matrix AA (AiA_i) and the jj column of the matrix BB (BjB_j) is equal to the matrix CijC_ {ij}:

cij=(Ai,Bj)c_{ij}=(A_{i},B_{j})

Matrix multiplication is possible if the width of the first matrix AA (𝑚×𝑛𝑚×𝑛) is equal to the height of the second matrix BB (𝑛×r𝑛×r). Then the dimensions of their product will be m×rm×r. We say that the dimension n "collapses".

Bb11b21Aa11a12c11Cc11=a11b11+a12b21\begin{aligned}\color{green} B\color{default} \enspace&\def\arraystretch{1.5}\begin{array} {|c|c|}\hline \color{green}b_{11} & \quad \\\hline\color{green}b_{21} \\\hline\end{array}\\&\enspace\Downarrow\\\color{crimson} A\color{default} \enspace\def\arraystretch{1.5}\begin{array} {|c|c|cc}\hline \color{crimson}a_{11} & \color{crimson}a_{12} \\ \hline\\ \hline \\ \hline\end{array}\thinspace\raisebox{1.75em}{$\Rarr$}\thinspace&\def\arraystretch{1.5} \begin{array} {|c|c|} \hline \color{skyblue}c_{11} \\\hline \\ \hline & \quad\\ \hline\end{array} \enspace \color{skyblue} C \end{aligned}\\\color{skyblue}c_{11} \color{default}= \color{crimson}a_{11}\color{default}\color{default}*\color{green}b_{11}\color{default}+\color{crimson}a_{12}\color{default}*\color{green}b_{21}

In numpy, matrices AA and BB are multiplied by calling the functions np.dot(A, B), or A.dot(B). You can also replace this call with a matrix multiplication sign @.

1import numpy as np
2
3print(A.dot(B))
4print(np.dot(A,B))
5print(A @ B)

The result of matrix multiplication depends on the order of multipliers. The result of matrix multiplication depends on the order of multipliers.

1matrix = np.array([
2 [1, 2, 3],
3 [-1, -2, -3]])
4
5print(matrix @ matrix)

An error occurred: the dimensions of the matrices are mismatched.

Multiplying the matrix by itself is only possible if it is square:

1square_matrix = np.array([
2 [1, 2, 3],
3 [-1, -2, -3],
4 [0, 0, 0]])
5
6print(square_matrix @ square_matrix)
Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,