FOR FREE MATERIALS

Checking different types of Matrices

Back to Programming

Description

This program is written to find the different types of matrices like:

  • Row matrix
  • Column matrix
  • Rectangular matrix
  • Square matrix
  • Diagonal matrix
  • Scalar Matrix

 

1. Row Matrix:

     A matrix is said to be row matrix if the matrix is a 1×n matrix i.e. the matrix has only a single row.

‘[=Let the matrix be:  A= [a, b, c]

Here, A is a matrix which has a single row. Therefore, A is a row matrix

 

2. Column Matrix:

    A matrix is said to be a column matrix if the matrix is a n×1 matrix i.e. the matrix has only a single column.

 

3. Rectangular Matrix:

A matrix is said to be a rectangular matrix if the number of rows is less than the number of columns or the number of columns is less than the number of rows. 

For example: 

Here, the matrix A is a 2×3 matrix. The number of rows is less than the number of columns. So, it is a rectangular matrix.

Here, the matrix A is a 3×2 matrix. The number of columns is less than the number of rows. So, it is a rectangular matrix.

 

 

4. Square Matrix:

A matrix is said to be a square matrix if the number of rows is equal to the number of columns. 

For example:

Here, A is a 2×2 matrix. As, the number of rows is equal to the number of columns of the matrix, this can be called as a square matrix.

 

 

5. Diagonal Matrix:

A matrix is said to be a diagonal matrix if only the principal diagonal elements are present in the matrix which may not be equal i.e. the elements may be different from each other. The other elements except for the principal diagonal of this matrix is 0.

For example:

Here, the principal diagonal is only present in the matrix, which is 10, 20 and 30. The other elements except these are 0. The principal diagonal elements are not equal to each other. Hence, A is a diagonal matrix.

 

 

6. Scalar Matrix:

A matrix is said to be a scalar matrix if only the principal diagonal elements are present in the matrix which should be equal i.e. the elements should be same to each other. The other elements except the principal diagonal of this matrix is 0.

For example:

 

Algorithm

INPUT: A matrix
OUTPUT: Type of the matrix
PROCESS:

Step 1: [taking the input]
	Read m, n [number of rows and columns of the matrix]
	For i=0 to m-1 repeat
		For j=0 to n-1 repeat
			Read a[i][j]
		[End of ‘for’ loop]
	[End of ‘for’ loop]
Step 2: [Checking and printing its type]
	If m=1 then
		Print "The matrix is a row matrix"
	[End of ‘if’]
	If n=1 then
		Print "The matrix is a column matrix"
	[End of ‘if’]
	If m<n or n<m then 
		Print "The matrix is a rectangular matrix"
	[End of ‘if’]
	If m=n then
		Print "The matrix is a square matrix"
	[End of ‘if’]
	For i = 0 to m-1 repeat 
        		For j = 0 to n-1 repeat 
  			if i ≠ j and a[i][j]≠0 then
        				print "Not a diagonal matrix"
        				Set f<-1
        				break
			[End of ‘if’]
		[End of ‘for’ loop]
	[End of ‘for’ loop]
	if f=0 then
		print "Diagonal matrix"
	[End of ‘if’]
	Set f<-0
	 for i = 0 to m-1 repeat 
        		for j = 0 to n-1 repeat 
            			if i≠j and a[i][j]≠0 then
            				print "Not a scalar matrix"
            				Set f<-1
            				break
			[End of ‘if’]
		[end of ‘for’ loop]
    		If f=0 then
    			For i = 0 to m – 2 repeat 
        				if a[i][i] ≠ a[i + 1][i + 1] then
         					print "Not a scalar matrix"
            					Set f<-1
            					break
				[End of ‘if’]
			[End of ‘for’ loop]
		[End of ‘if’]	 
    		If f=0
    			Print "Scalar matrix"
Step 3: Stop

Code

TIME COMPLEXITY:

if(m==1)------------------- O(c1)

                { printf("\nThe matrix is a row matrix");

                }

                if(n==1)------------------------ O(c2)

                { printf("\nThe matrix is a column matrix");

                }

                if(m<n||n<m)---------------- O(c3)

                { printf("\nThe matrix is a rectangular matrix");

                }

                if(m==n)------------------------ O(c4)

                { printf("\nThe matrix is a square matrix");

                }

                //checking for diagonal matrix

                for(i = 0; i < m; i++) ----------- O(m)

                  for(j = 0; j <n; j++) ---------- O(n)

                       if ((i != j) && (a[i][j] != 0)) ---------- O(c5)

                         {

                                printf("\nNot a diagonal matrix");

                                f=1;

                                break;

                                                }

                        if (f==0)------------------ O(c6)

                         { printf("\nDiagonal matrix");

                          }

                //checking for scalar matrix

                 f=0;----------------------------- O(1)

                 for (i = 0; i < m; i++) ------- O(m)

                 for (j = 0; j < n; j++) ------- O(n)

            if ((i != j) && (a[i][j] != 0)) -------- O(c7)

            { printf("\nNot a scalar matrix");

                f=1;

                break;  }

    //Check all diagonal elements are same or not. 

    if(f==0)---------------- O(c8)

    for (i = 0; i < m - 1; i++)---------- O(m-1) 

        if (a[i][i] != a[i + 1][i + 1]) ------------ O(n-1)

        {  printf("\nNot a scalar matrix");

            f=1;

            break; } 

         if(f==0)--------------- O(c9)

                printf("\nScalar matrix");

 

Therefore, the complexity is O(m*n). If the number of rows and columns are the same, then the complexity will be O(n2).

 

APPLICATIONS:

  1. The diagonal matrix is used in many areas of linear algebra.
  2. A square matrix can be used to represent a linear transformation of a geometric object.