Call Us On : +91 9873543020    [email protected]

## Matrix basics for MATLAB

##### Welcome to the series of blogs on Digital Image Processing using MATLAB. If you are looking for complete guidance in understanding the concepts of the digital images and image processing, you’re at the right place! In this series, we will learn the concepts of Image Processing along with the implementation from scratch to the advanced level. In Matrix basics for MATLAB, we will discuss the basics and key points of the matrix in context to MATLAB. So let’s continue!

In the previous section, we have learned the Digital Image Fundamentals that describes the basics of digital images. In this section- Matrix basics for MATLAB, we will learn about the fundamentals of Matrix which is necessary as the prerequisite for understanding MATLAB.

A matrix is an mxn array/group of finite numbers. Here m and n are the numbers of rows and columns of the matrix respectively. The finite numbers in the matrix are called the elements of the matrix.

Example:

Here A, B, and C are the names of the matrices (matrix, in plural) and the numbers within the brackets are the elements of the matrices.  A is a 2×2 matrix (2 rows and 2 columns), B is a 3×3 matrix and matrix C has dimension 3×2.

In general, a matrix is represented as follows:

Where M is the matrix name and the elements of the matrix are denoted as aij (1 ≤ i ≤ m, 1 ≤ j ≤ n). Conventionally, we use the uppercase letter for denoting a matrix and the lowercase for the elements. aij represents the element at the ith row and jth column. For example, a34 denotes an element of the matrix at the 3rd row and 4th column. The dimension of the matrix M is m x n.

## Some special matrices

• Square matrix: If m=n, that is the number of rows and columns are equal, we call it a square matrix. For example,

Here A is a 2×2 and B is a 3×3 square matrix.

• Diagonal Matrix: If all the non-diagonal elements of a matrix are 0, we refer it as a Diagonal Matrix. Technically, a Diagonal Matrix fulfills the following condition: aij = 0 for ij. For example,
• Identity Matrix: An identity matrix is a diagonal matrix with all the diagonal elements equal to 1. For example,
• Zero/Null Matrix: A Zero matrix has all the elements equal to 0. For example,

## Matrix Operations

We can carry out various operations using matrices for different purposes. Some of them are similar to normal arithmetic operations while some need special rules. A few basic matrix operations are as follows.

• Addition: We can add two matrices A and B if they are equal in dimensions. The resultant matrix C is the element-wise addition of the two matrices. The size of the output matrix is the same as the input matrices. The output matrix C is thus defined as

C = A + B

Ex:

• Subtraction: The condition for the subtraction of matrices is the same as for addition in terms of dimensions. Using the above example and notations, we perform subtraction of matrices as follows.
• Multiplication: Matrix multiplication is a bit different in terms of dimension conditionality and element-wise operations. There are two types of multiplications involving matrices: Scalar multiplication and Matrix multiplication.
1. #### Scalar multiplication:

If we multiply a matrix by a single number (called scalar), we refer it scalar multiplication. In this case, we multiply each of the elements of the matrix by the scalar number individually. The input matrix can be of any dimension. We can use the following equation to represent the scalar multiplication.

C = k.A

Where k is a constant/number. For example, consider the matrices A and B from the above examples.

2. #### Matrix Multiplication

This involves the multiplication of a matrix by another matrix. Two matrices can be multiplied if and only if “the number of columns in the first matrix is equal to the number of rows in the second matrix”. The output matrix dimension would be r1xc2 (r1 – rows in the first matrix, c2 – columns in the second matrix). For example,

Multiplication procedure

Mathematically speaking, two matrices are multiplied by taking the dot product of corresponding rows and columns of the matrices. However, for those who don’t have enough mathematical background, it would be better to understand the concept using the above example.

We obtain the element cij of the output matrix C by multiplying the ith row of matrix A and the jth column of matrix B. Fig. 2.11 depicts a generalized example of the formation of the multiplication matrix by taking a 2×3 and a 3×2 matrix. We can observe the calculation of the elements of the output matrix one by one.

Finally, take a numerical example to understand the matrix multiplication procedure.

• Transpose – If we interchange the rows and columns of a matrix, we call it the transpose of the original matrix. Mathematically, matrix B is the transpose of matrix A if bij= aij. Matrix B is then denoted as B = A’ or B =AT.

### The matrix in the context of MATLAB

Various programming languages, including MATLAB use slightly different terminologies for addressing some of the Matrix parameters and operations. We will discuss a few of them here.

#### Vectors:

A vector is a one-dimensional array/matrix. Therefore, we can refer a vector as a special case of a matrix having only one row or one column. For example,

Here matrix A is a row vector and matrix B is a column vector. If we address A and B as matrices, we say matrix A has dimension 1x3 while matrix B has dimension 3x1. On the other hand, if we address it as a vector, we say A is a row vector of length 3 while B is a column vector of length 3.

#### Indexing

An index is used to specify one or more elements of a matrix or an array. For example, we can specify some elements of vector A and B of Fig. 2.14 as follows: index of a2 in vector A is 2, the index of bin vector B is 3. MATLAB usually uses two types of indexing to address the matrix elements.

1. #### Linear indexing:

Linear indexing uses only one subscript to specify an element. So whether the array is a one-dimensional vector or a multi-dimensional matrix, just a single number is sufficient to address any of the elements of the array.
In the case of a vector (row or column), it’s simple! Starting from the first element with index as 1, the subsequent elements are having the index equal to its position in the array. For example,

You should also observe the way indexing is represented in MATLAB. First, we write the name of the matrix. This follows a small bracket and within the bracket, we have the index of an element. On the right-hand side of the equality, we write the value of the element. In general,

Matrix_name (index) = element_value

Note that not all programming languages use the same notations for indexing. Some of them (like python) use square bracket [ ] for addressing the array elements.

For a matrix indexing, however, you need to pay a little more attention. MATLAB considers column-wise indexing for a matrix. That is, it treats a matrix as a long vertical string of elements. We can assume this as each column of the matrix is stacked below the previous column. Then the indexing procedure is the same as for a vector. Fig. 2.16 illustrates the linear indexing for a 3×3 matrix.

Note that the column vector (on the right) is just to depict the indexing method. MATLAB does not literally convert the matrices into a column vector for indexing.

2. #### Indexing using subscript

In this case, we use multiple subscripts for addressing the elements of an array. we do the matrix indexing using two subscripts, one for the rows and other for the columns.  Consider the example of Fig 2.16, where we have used linear indexing for a 3×3 matrix. The subscript indexing is simple in the sense that in this case, we address the elements using their row and column position in the array. Fig. 2.17 shows the subscript indexing for the same matrix.

Note that we can use subscript indexing to address the vector elements as well. The subscript indexing for the vector from Fig. 2.15, is as follows.

We’ll discuss the Indexing of MATLAB arrays in more detail in the next sections of the blog.

#### Three-dimensional Matrix:

So far we have discussed two types of matrices in terms of their dimensions: 1-D matrix (vector) and 2-D matrix. Vectors have either a row or a column as their dimension and in general, a matrix has both rows and columns. A three-dimensional matrix/array has three dimensions: rows, columns, and planes. That is, a three-dimensional matrix is a collection of two-dimensional matrices of the same dimensions. Each of these 2-D matrices is termed as the plane of the 3-D matrix.

Fig. 2.19 depicts a typical three-dimensional matrix. Note that the number of rows and columns are the same for all planes. Here three planes are shown, however, a 3-D matrix can have any number of planes. 3-D matrices are quite important in order to understand the concept of color images in Image Processing. A color digital image comprises three planes of matrices representing three color components- Red, Green, and Blue respectively.

3-D matrix indexing:

Since a 3-D matrix has three dimensions (rows, columns, and planes), 3 distinct subscripts are needed for indexing. However, linear indexing still requires only one number for addressing an element of a 3D matrix. An example of 3-D matrix indexing for a 3x4x3 matrix is depicted in Fig. 2.20 where indices are shown at their corresponding elements’ place.

The first and the second subscript for each of the elements is indicating its rows and the columns’ position respectively. The third one is indicating the plane position of the elements.

By now we have developed sufficient background for the basic understanding of MATLAB. In the next blog, we will discuss the basics of MATLAB in the context of image processing.

Thank you.

Contact us!

For training, Research Assistance, and Consultancy Services on Computer Vision, Deep Learning, Signal Processing, Image Processing, and Medical Signal/Image Processing.