A matrix is a group of numbers or elements which are arranged as a rectangular array. The matrix's rows and columns are usually indexed by letter. For a *n x m* matrix, n represents the number of rows and m represents the number of columns. If we have a hypothetical *n x m* matrix, it will be structured as follows:

If *n* *= m,* then it is* *called a square matrix:

A vector is actually a matrix with one row or one column with more than one element. It can also be defined as the *1-by-m* or *n-by-1* matrix. You can interpret a vector as an arrow or direction in an *m* dimensional space. Generally, the capital letter denotes a matrix, like *X* in this example, and lowercase letters with subscripts like *X _{11}* denote the element of the matrix

*X*.

In addition, there are some important special matrices: the zero matrix (null matrix) and the identity matrix. *0* denotes the zero matrix, which is a matrix of all *0*s (MacDufee 1943 p.27). In a *0* matrix, it's optional to add subscripts:

The identity matrix denoted by *I*, and its diagonal elements are *1* while the others are *0*:

When you multiply a matrix *X* with the identity matrix, the result will be equal to *X*:

An identity matrix is very useful for calculating the inverse of a matrix. When you multiply any given matrix with its inverse, the result will be an identity matrix:

Let's briefly see the matrix algebra on NumPy arrays. Addition and subtraction operations for matrices are similar to math equations with ordinary single numbers. As an example:

Scalar multiplication is also pretty straightforward. As an example, if you multiply your matrix *X* by *4*, the only thing that you should do is multiply each element with the value *4* as follows:

The seemingly complicated part of matrix manipulation at the beginning is matrix multiplication.

Imagine you have two matrices as *X*_{ }and *Y*, where *X* is an matrix and *Y* is an matrix:

The product of these two matrices will be as follows:

So each element of the product matrix is calculated as follows:

Don't worry if you didn't understand the notation. The following example will make things clearer. You have matrices *X *and

*Y*and the goal is to get the matrix product of these matrices:

The basic idea is that the product of the *i _{th}* row of

*X*and the

*j*

_{th }of column

*Y*will become the

*i*element of the matrix in the result. Multiplication will start with the first row of

_{th}, j_{th}*X*and the first column of

*Y*, so their product will be Z

*[1,1]*:

You can cross-check the results easily with the following four lines of code:

In [1]: import numpy as np

x = np.array([[1,0,4],[3,3,1]])

y = np.array([[2,5],[1,1],[3,2]])

x.dot(y)

Out[1]: array([[14, 13],[12, 20]])

The previous code block is just a demonstration of how easy to calculate the dot product of two matrices by use of NumPy. In later chapters, we will go more in deep into matrix operations and linear algebra.