Basic Matrix Operations


We learnt about matrix definitions . In this chapter we are going to learn about various matrix operations.

I. Matrix Addition & Subtraction
Two matrices can be added if and only if they are of the same order. We $cannot$ add two matrices that are of different orders.
Let us take an example of adding two matrices.
Let's take a $2 \times 3$ matrix $A$ as:
$A = [a_{i,j}]_{2\times 3} = \begin{bmatrix}2 & 3 & 4 \\ 5 & 6 & 7\end{bmatrix}$

and another $2 \times 3$ matrix $B$ as:
$B = [b_{i,j}]_{2\times 3} = \begin{bmatrix}8 & 9 & 10 \\ 11 & 12 & 13\end{bmatrix}$

Since both $A$ and $B$ are of the same order, it is possible to add them and the result will be a matrix of the same order that is $2 \times 3$., that is,
$A + B = C = [c_{i,j}]_{2\times 3}$
Each element of the resultant matrix $C$ can be calculated by just adding the corresponding elements of $A$ and $B$, that is,
$c_{1,1} = a_{1,1} + b_{1,1} = 2 + 8 = 10$
$c_{1,2} = a_{1,2} + b_{1,2} = 3 + 9 = 12$
$c_{1,3} = a_{1,3} + b_{1,3} = 4 + 10 = 14$
$c_{2,1} = a_{2,1} + b_{2,1} = 5 + 11 = 16$
$c_{2,2} = a_{2,2} + b_{2,2} = 6 + 12 = 18$
$c_{2,3} = a_{2,3} + b_{2,3} = 7 + 13 = 20$

-----------book page break-----------
Now, we have our answer which is:
$A + B = \begin{bmatrix}2 & 3 & 4 \\ 5 & 6 & 7\end{bmatrix} + \begin{bmatrix}8 & 9 & 10 \\ 11 & 12 & 13\end{bmatrix} = \begin{bmatrix}10 & 12 & 14 \\ 16 & 18 & 20\end{bmatrix}$

We can perform the subtraction operation in exactly the same way, by subtracting the corresponding elements of the two matrices:
$A - B = \begin{bmatrix}2 & 3 & 4 \\ 5 & 6 & 7\end{bmatrix} - \begin{bmatrix}8 & 9 & 10 \\ 11 & 12 & 13\end{bmatrix} = \begin{bmatrix}-6 & -6 & -6 \\ -6 & -6 & -6\end{bmatrix}$


II. Scalar Multiplication
In this case a matrix is multiplied by a scalar value. The result is obtained by multiplying each of the elements of the matrix by the scalar value.
Let's take a $3 \times 2$ matrix $A$ as:
$A = \begin{bmatrix}4 & -7 \\ -2 & -5 \\ 8 & 3\end{bmatrix}$

How much is $(-3) \times A$?
The resulting matrix will be obtained by multiplying each of the elements of $A$ by $-3$, hence we get:

$-3A = -3 \cdot \begin{bmatrix}4 & -7 \\ -2 & -5 \\ 8 & 3\end{bmatrix} = \begin{bmatrix}-3 \times 4 & -3 \times -7 \\ -3 \times -2 & -3 \times -5 \\ -3 \times 8 & -3 \times 3\end{bmatrix} = \begin{bmatrix}-12 & 21 \\ 6 & 15 \\ -24 & -9\end{bmatrix}$

-----------book page break-----------
In general the multiplication of a matrix $A_{m \times n}$ by a scalar quantity $k$, is defined as:
$k  [A_{m \times n}]$ 

$= k \begin{bmatrix} a_{1, 1} & a_{1, 2} & ... & a_{1, n} \\ a_{2, 1} & a_{2, 2} & ... & a_{2, n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m, 1} & a_{m, 2} & ... & a_{m, n} \end{bmatrix}$

$= \begin{bmatrix} k \cdot a_{1, 1} & k \cdot a_{1, 2} & ... & k \cdot a_{1, n} \\ k \cdot a_{2, 1} & k \cdot a_{2, 2} & ... & ka_{2, n} \\ \vdots & \vdots & \ddots & \vdots \\ k \cdot a_{m, 1} & k \cdot a_{m, 2} & ... & k \cdot a_{m, n} \end{bmatrix}$

III. Properties Of Scalar Multiplication
Distributive Over Addition/Subtraction
That is, $k(A_{m,n} \pm B_{m,n}) = kA_{m,n} \pm kB_{m,n}$

Commutative
That is, $kA_{m,n} = A_{m,n} k$

Associative
That is, $p \cdot (q \cdot A_{m,n}) = (p \cdot q) \cdot A_{m,n}$

-----------book page break-----------
IV. Matrix Multiplication
Now we will see what how to multiply two matrices. Two matrices can be multiplied if, and only if, the number of columns of the first matrix is equal to the number of rows in the second matrix. If we have two matrices

$A = \begin{bmatrix}a_{i,j}\end{bmatrix}_{m \times n}$    and
$B = \begin{bmatrix}b_{i,j}\end{bmatrix}_{p \times q}$

it is only possible to evaluate $A \times B$ only if $n = p$, and the result will be a matrix $P$ with an order of $m \times q$. It is possible to evaluate $B \times A$ only if $q = m$ and the resultant matrix will be of order $p \times n$

Since the number of columns in the first matrix is the same as the number of rows in the second matrix, each row of the first matrix will contain the same number of elements as each column of the second matrix. Each element of the resulting matrix is obtained by multiplying the elements of the corresponding row of the first matrix and the corresponding column of the second matrix, and taking the sum of these products.

Let's take an example and understand this better.
Let us consider matrix $A$ as a $3 \times 2$ matrix and matrix $B$ as $2 \times 3$ matrix. Observe, that the number of columns of $A$ is $2$ and the number of rows in matrix $B$ is also $2$, hence we should be able to multiply these two matrices.

$A = \begin{bmatrix}3 & -1 \\ 2 & 4 \\ -3 & 5\end{bmatrix}$     this is a matrix with $3$ rows and $2$ columns

$B = \begin{bmatrix}-2 & 5 & -1 & 3 \\ 3 & 6 & -4 & 1\end{bmatrix}$    this is a matrix with $2$ rows and $4$ columns

-----------book page break-----------
As stated before, the resulting matrix will have $3$ rows and $4$ columns
So, let us write the complete expression with the resultant matrix filled with variables:

$AB = P$

$\Rightarrow \begin{bmatrix}3 & -1 \\ 2 & 4 \\ -3 & 5\end{bmatrix} \begin{bmatrix}-2 & 5 & -1 & 3 \\ 3 & 6 & -4 & 1\end{bmatrix} = \begin{bmatrix}\color{#999999}{\begin{array}{cccc}p_{1,1} & p_{1,2} & p_{1,3} & p_{1,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{2,1} & p_{2,2} & p_{2,3} & p_{2,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{3,1} & p_{3,2} & p_{3,3} & p_{3,4}\end{array}}\end{bmatrix}$

Now we will see how to find the values of each element of the result matrix $P$.
To find the value of the top-right corner element $p_{1,1}$, we will need to take the elements of $1\xasuper{st}$ row of $A$ and the $1\xasuper{st}$ column of $B$ as shown in the step below (the selected row and column are highlighted in blue):

$\begin{bmatrix}\enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{cc}\ 3 & -1\end{array}}} \\ \begin{array}{cc}2 & 4\end{array} \\ \begin{array}{cc}-3 & 5\end{array} \end{bmatrix} \begin{bmatrix}\enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{c}-2 \\ 3\end{array}}} & \begin{array}{c}5 \\ 6\end{array} & \begin{array}{c}-1 \\ -4\end{array} & \begin{array}{c}3 \\ 1\end{array}\end{bmatrix} = \begin{bmatrix}\color{#999999}{\begin{array}{cccc}\enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{p_{1,1}}} & p_{1,2} & p_{1,3} & p_{1,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{2,1} & p_{2,2} & p_{2,3} & p_{2,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{3,1} & p_{3,2} & p_{3,3} & p_{3,4}\end{array}}\end{bmatrix}$
 
Thus we get $p_{1,1} = (3)\times(-2) + (1) \times (-3) = -6 + (-3) = -9$

Now we can replace $p_{1,1}$ with $-9$ and move on to finding $p_{1,2}$

To find $p_{1,2}$ we need to select the $1\xasuper{st}$ row of $A$ and the $2\xasuper{nd}$ column of $B$, as highlighted below in blue, and perform the same multiplication of corresponding elements and take the sum, as follows:

-----------book page break-----------
$\begin{bmatrix}\enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{cc}\ 3 & -1\end{array}}} \\ \begin{array}{cc}2 & 4\end{array} \\ \begin{array}{cc}-3 & 5\end{array} \end{bmatrix} \begin{bmatrix}\begin{array}{c}-2 \\ 3\end{array} & \enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{c}5 \\ 6\end{array}}} & \begin{array}{c}-1 \\ -4\end{array} & \begin{array}{c}3 \\ 1\end{array}\end{bmatrix} = \begin{bmatrix}\color{#999999}{\begin{array}{cccc}\color{black}{-9} & \enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{p_{1,2}}} & p_{1,3} & p_{1,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{2,1} & p_{2,2} & p_{2,3} & p_{2,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{3,1} & p_{3,2} & p_{3,3} & p_{3,4}\end{array}}\end{bmatrix}$

$p_{1,2} = 3 \times 5 + (-1) \times 6 = 15 - 6 = 9$

Likewise we can find $p_{1,3}$ using row $1$ of matrix $A$ and column $3$ of matrix $B$ as follows:
$p_{1,3} = 3 \times (-1) + (-1) \times (-4) = -3 + 4 = 1$,

To find $p_{1,4}$ we need to use row $1$ of matrix $A$ and column $4$ of matrix $B$.
$p_{1,4} = 3 \times 1 + (-1) \times 1 = 3 - 1 = 2$

To find $p_{2,1}$ we need to use row $2$ of matrix $A$ and column $1$ of matrix $B$.
$p_{2,1} = 2 \times (-2) + 4 \times 3 = -4 + 12 = 8$

To find $p_{2,2}$ we need to use row $2$ of matrix $A$ and column $2$ of matrix $B$.
$p_{2,2} = 2 \times (5) + 4 \times 6 = 10 + 24 = 34$

Just to make sure you have understood these steps, we will find one more value using highlighted row and column as before.
To find $p_{2,3}$ we need to use row $2$ of matrix $A$ and column $3$ of matrix $B$ as highlighted below:

$\begin{bmatrix}\begin{array}{cc}\ 3 & -1\end{array} \\ \enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{cc}2 & 4\end{array}}} \\ \begin{array}{cc}-3 & 5\end{array} \end{bmatrix} \begin{bmatrix}\begin{array}{c}-2 \\ 3\end{array} & \begin{array}{c}5 \\ 6\end{array} & \enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{\begin{array}{c}-1 \\ -4\end{array}}} & \begin{array}{c}3 \\ 1\end{array}\end{bmatrix} = \begin{bmatrix}\color{#000000}{\begin{array}{cccc}-9_{\ \ \ } & 9_{\ \ \ } & \ \ 1 & \ \ 2 \end{array}} \\ \color{#999999}{\begin{array}{cccc}\color{black}{\ \ \ \ }8 & \color{black}{\ _{\ \ \ }34} & \enclose{box}[mathcolor="#39d0f9",mathbackground="#39d0f9"]{\color{black}{p_{2,3}}} & p_{2,4}\end{array}} \\ \color{#999999}{\begin{array}{cccc}p_{3,1} & p_{3,2} & p_{3,3} & p_{3,4}\end{array}}\end{bmatrix}$

Using the row and the column shown above we get:
$p_{2,3} = 2 \times (-1) + 4 \times (-4) = -2 + (-16) = -18$

-----------book page break-----------
Likewise, we can calculate the remaining elements of $P$.
$p_{2,4} = 2 \times 3 + 4 \times 1 = 6 + 4 = 10$
$p_{3,1} = (-3) \times (-2) + 5 \times 3 = 6 + 15 = 21$
$p_{3,2} = (-3) \times 5 + 5 \times 6 = -15 + 30 = 15$
$p_{3,3} = (-3) \times (-1) + 5 \times (-4) = 3 + (-20) = -17$
$p_{3,4} = (-3) \times 3 + 5 \times 1 = -9 + 5 = -4$

Replacing all these values in $P$ we get our final answer as:

$\begin{bmatrix}3 & -1 \\ 2 & 4 \\ -3 & 5\end{bmatrix} \begin{bmatrix}-2 & 5 & -1 & 3 \\ 3 & 6 & -4 & 1\end{bmatrix} = \begin{bmatrix}-9 & 9 & 1 & 2 \\ 8 & 34 & -18 & 10 \\ 21 & 15 & -17 & -4 \end{bmatrix}$

Now that we have understood the process to compute the product of two matrices, let us answer the question:
Why is it so, that the first matrix should have the same number of columns as the number of rows in second matrix? 
The answer should be clear by now, that is, if the number of columns in the first matrix was different from the number of rows in the second matrix, then rows of the first matrix would contain a different number of elements as compared to the number of elements in the columns of the second matrix. It would not be possible to find the product of corresponding elements and their sum.

Similarly, if we want to test the commutative property of matrix multiplication, we can try to multiply $B A$ from our example.

$BA$
$= \begin{bmatrix}-2 & 5 & -1 & 3 \\ 3 & 6 & -4 & 1\end{bmatrix}_{2\times 4} \begin{bmatrix}3 & -1 \\ 2 & 4 \\ -3 & 5\end{bmatrix}_{3 \times 2}$

-----------book page break-----------
Now we can see that each row of the first matrix contains $4$ elements while each column of the second matrix contains $3$ element. Hence, it is not possible to perform a row and column wise multiplication, therefore, $BA$ does not exist.
In general, matrix multiplication is not commutative.

Now that you have understood how to multiply two matrices, you can use the widget below to do some rapid practice of matrix multiplication.
--------- Reference to widget: 20bd18ed-be06-447e-aca2-0dca9fd56b6d ---------


-----------book page break-----------
V. Division
The concept of division, in the traditional number division sense, does not exist for matrices.
However, there is the concept of multiplication by inverse which yields similar result as division. For example, if you have two numbers $x$ and $y$, such that $x \cdot y = 1$, then $y = \dfrac{1}{x} = x^{-1}$
Similarly, inverse of a square matrix is such, that when multiplied with the matrix the result is the identity matrix.
That is, if,
$A \times B = I$, where $I$ is the identity matrix, then $B$ is called the inverse of $A$ and is denoted by $A^{-1}$

VI. Properties Of Matrix Multiplication
Associativity
Matrix multiplication is associative, that is:
$\left([a_{ij}]_{m \times n} \times [b_{ij}]_{p \times q} \right) \times [c_{ij}]_{r \times s} = [a_{ij}]_{m \times n} \times \left([b_{ij}]_{p \times q} \times [c_{ij}]_{r \times s} \right)$

Note, that the above product exists only if $n = p$ and $q = r$

Distributive Property
Matrix multiplication is distributive over addition and subtraction, that is:
$[a_{ij}]_{m \times n} \times \left([b_{ij}]_{p \times q} \pm [c_{ij}]_{r \times s} \right) = [a_{ij}]_{m \times n} \times [b_{ij}]_{p \times q} \pm [a_{ij}]_{m \times n} \times [c_{ij}]_{r \times s}$

Note, the above multiplication is valid only if $n = p = r$ and $q = s$

Commutative Property
Matrix multiplication is not commutative in general. That is $AB \ne BA$. 

-----------book page break-----------
However, there are a few exceptions, where commutative property holds, for special cases. We will list out some of those special cases here.
If both matrices are square matrices, and one of the matrices is a scalar matrix (that is, the identity matrix multiplied by a scalar), then the multiplication is commutative, that is:
$A \times S = S \times A$ where $S$ is a scalar matrix.

For example,
$\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix} \times \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix} = \begin{bmatrix} 14 & 16 \\ 18 & 20 \end{bmatrix} =  \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix} \times \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}$

The reason for this is easy to see. Any scalar matrix $S$ can be written as:
$S = \lambda \times I$ where $I$ is the identity matrix
In our example, we can write $\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix} = 2 \times \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}$

Therefore, our multiplication becomes:
$\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix} \times \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix}$

$= 2 \times \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \times \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix} = 2 \times \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix}$

Our multiplication has reduced to a simple scalar multiplication, and is therefore, commutative.

The other exception is multiplication by inverse. That is, if for a square matrix $A$, its inverse $A^{-1}$ exists, then,
$A \times A^{-1} = A^{-1} \times A = I$

-----------book page break-----------
VII. Matrix Exponentiation
We saw in the previous section how to multiply two matrices. In this section we will see how to compute the power of a matrix.
It is possible to calculate the $n\xasuper{th}$ power of a matrix $A$ only if it is a square matrix. We will soon find out why? But first let us understand how.
Like exponentiation of scalar values is defined as repeated multiplication of the value by itself, matrix exponentiation has the exact same definition. That is, repeated multiplication of the matrix by itself. Now let us take an example of this.

Lets say we have a matrix $A_{2\times 2}$ as:

$A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$

 and we need to calculate $A^3$. We can write:
$A^3$
$= \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \times \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \times \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$

$= \begin{bmatrix} 7 & 10 \\ 15 & 22 \end{bmatrix} \times \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$

$= \begin{bmatrix} 37 & 54 \\ 81 & 118 \end{bmatrix}$

Notice that we have computed the product of the three matrices starting with the leftmost matrix. Matrix multiplication is associative, therefore, if we had multiplied the second and the third matrix first and we would have gotten the same result.

-----------book page break-----------
Now let us understand why we cannot compute the power of a matrix $A_{m \times n}$ which is not a square matrix, that is, $m \ne n$

Let,
$A_{2 \times 3}  = \begin{bmatrix}5 & -1 & 3 \\ -2 & 4 & 0\end{bmatrix}$

$\therefore A^2 = A \times A = \begin{bmatrix}5 & -1 & 3 \\ -2 & 4 & 0\end{bmatrix} \begin{bmatrix}5 & -1 & 3 \\ -2 & 4 & 0\end{bmatrix}$

Here we can see that the number of columns in the first matrix is $3$ and the number of rows in the second matrix is $2$, hence they do not fulfil the requirement for matrix multiplication, that requires that the number of columns in the first matrix be equal to the number of rows in the second matrix. Therefore, $A^2$ does not exist.
Therefore, we can say that a matrix can only be multiplied by itself, if it is a square matrix.

VIII. Matrix Transpose
Given a matrix $A_{m \times n}$ of order $m \times n$, if we change all its rows into columns, the resultant matrix will be a matrix of order $n \times m$. This matrix is called the transpose of $A$ and is denoted by $A^T$.

If we take $A = \begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}$, then,
$A^T = \begin{bmatrix}1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}$

-----------book page break-----------
Symmetric Matrix
If for a matrix $A$, if $A^T = A$, then it is a symmetric matrix. For example, if,
$A = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 5 & 6 \\ 3 & 6 & 9\end{bmatrix}$, then,
$A^T = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 5 & 6 \\ 3 & 6 & 9\end{bmatrix}$

Therefore, $A^T = A$ and $A$ is a symmetric matrix.
Symmetric matrices are always square, and for all $i, j$, $a_{ij} = a_{ji}$

Skew Symmetric Matrix
If for any given matrix, $A^T = -A$, then it is a skew symmetric matrix.
For a skew symmetric matrix,
$a_{ij} = -a_{ji}$ for all $i \ne j$ and for all $i = j$, $a_{ij} = 0$, in other words, all diagonal elements should be $0$.

For example,
if $A = \begin{bmatrix} 0 & -2 & -3 \\ 2 & 0 & -6 \\ 3 & 6 & 0\end{bmatrix}$, then,
$A^T = \begin{bmatrix} 0 & 2 & 3 \\ -2 & 0 & 6 \\ -3 & -6 & 0\end{bmatrix} = -\begin{bmatrix} 0 & -2 & -3 \\ 2 & 0 & -6 \\ 3 & 6 & 0\end{bmatrix}$



-----------book page break-----------
IX. Properties Of Transpose
Transpose Of Transpose
$(A^T)^T = A$

It is easy to see that changing rows to columns twice, will give us the original matrix back.
If $A = \begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}$, then,
$A^T = \begin{bmatrix}1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}$, and,
$(A^T)^T = \begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}$

Distributive Property
Transpose of a matrix is distributive over addition and subtraction, that is:
$(A \pm B)^T = A^T \pm B^T$

Multiplication Of Transposes
If we have $A_{m \times n}$, then the transpose is ${A^T}_{n \times m}$, therefore,
$P = AA^T$ will be an $m \times m$ square matrix, and,
$P' = A^TA$ will be an $n \times n$ square matrix.