Call Us On : +91 9873543020    [email protected]

Matrix: Basics and Usage in MATLAB

In the previous section of the blog, I’ve discussed the Digital Image Fundamentals that describes the basics, structure, and types of digital images. In this section, we will learn about the fundamentals of Matrix which is necessary as the prerequisite for understanding MATLAB.

A matrix is an mxn array of finite numbers each drawn from a field F. m and n are referred to as the number of rows and columns of the matrix respectively, and each of the finite numbers is called the element of the matrix.


Fig. 2.1 Matrix Examples

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 the dimension 3×2.

In general, a matrix is represented as follows:

Fig. 2.2 Matrix Representation

Where M is the matrix name and the elements of the matrix are denoted as aij (1 ≤ i ≤ m, 1 ≤ j ≤ n). Conventionally, uppercase is used for denoting a matrix while the lowercase is used for the elements. aij represents the element at 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, it is called a square matrix.


Fig. 2.3 Square Matrix

 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, it is called a Diagonal Matrix. Technically, a Diagonal Matrix fulfills the following condition: aij = 0 for ij.


Fig. 2.4 Diagonal Matrix
  • Identity Matrix: A diagonal matrix having all the diagonal elements equal to 1 is known as the identity matrix.


Fig. 2.5 Identity Matrix
  • Zero/Null Matrix: If all the elements of a matrix are 0, it is called a Zero matrix.


Fig. 2.6 Zero/Null Matrix

Matrix Operations

Various operations are carried out 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 described below.

  • Addition: Two matrices A and B can be added together if they are equal in dimensions. The resultant matrix C is obtained by 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


Fig. 2.7 Matrix Addition
  • Subtraction: The condition for the subtraction of matrices is the same as for addition in terms of dimensions. Using the above example and notations, the subtraction of matrices can be visualized as follows.


Fig. 2.8 Matrix Subtraction
  • 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 a matrix is multiplied by a single number (called scalar), it is termed as Scalar multiplication. In this case, each of the elements of the matrix is multiplied by the scalar number individually. The input matrix can be of any dimension. Scaler multiplication can be represented by the following equation.

      C = k.A

      Where k is a constant/number. Ex: Consider the matrices A and B from the above examples.

      Fig. 2.9 Scalar Multiplication
    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).

      Fig. 2.10 Matrix Multiplication


      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.

      The element cij of the output matrix C is obtained by multiplying the ith row of matrix A and jth column of matrix B. A generalized example of the formation of multiplication matrix by taking a 2×3 and a 3×2 matrix is depicted below. All the elements of the output matrix are calculated and shown one by one in the subsequent images.

      Fig. 2.11 Element-wise Matrix multiplication


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

      Fig. 2.12 Matrix Multiplication: Numerical example

Matrix in the context of MATLAB

Various programming languages, including MATLAB use slightly different terminologies for addressing some of the Matrix parameters and operations. A few of them are discussed here.

  • Vectors: A vector is referred to as a one-dimensional array. In other words, a matrix with only one row or one column is called a vector. Ex:
    Fig. 2.13 Vectors

    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. However, if they are addressed as a vector, we say A is a row vector of length 3 while B is a column vector of length 3.

  • Indexing: Index is used to specify one or more elements of a matrix or an array. For example, some elements of vector A and B of Fig. 2.13 can be specified as follows: index of a2 in vector A is 2, 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 used 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. Ex:

      Fig. 2.15 Vector Indexing

      You should also observe the way indexing is represented in MATLAB. First, the name of the matrix is written followed by a small bracket having the index and on the right-hand side of the equality, the value of the element is written. 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, 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 such that each column of the matrix is stacked below the previous column and then indexing is done exactly the same as for a vector. Ex:

      Fig. 2.16 Matrix Indexing


      Note that the column vector (on the right) is shown 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, elements of an array are addressed using multiple subscripts. Mostly, matrix indexing is done using two subscripts, one is used for the rows and other for the columns.  Consider the example of Fig 2.16, where a 3×3 matrix is shown using linear indexing. The subscript indexing is simple in the sense that here elements are addressed by their row and column position in the array.
      Fig. 2.17 Subscript Indexing

      Note that the subscript indexing can be used to address the vector elements also. The vector indexing example from Fig. 2.15, using subscript indexing can be described as follows.

      Fig. 2.18 Subscript Indexing (vector)

      Indexing of MATLAB arrays will be discussed in more detail in the next section 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 3D 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 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.
    Fig. 2.20 3D matrix indexing

    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.

Close Menu