FOR FREE YEAR SOLVED

Transpose of a Matrix

Back to Programming

Description

The transpose of a matrix can be defined as a new matrix whose rows are the columns of the original matrix and the columns are the rows of the original matrix. If A is a matrix then its transpose is denoted as AT.

 

For example:

If a matrix is:

 

Then the transpose of the matrix is

Algorithm

INPUT: A matrix
OUTPUT: The transpose of the matrix

PROCESS:
Step 1: [taking the input]
	Read m, n [the 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: [The transpose of the matrix]
	for i = 0 to m-1 repeat
        		for j = 0 to n-1 repeat
        			Set t[j][i]<-a[i][j]
		[End of ‘for loop]
	[End of ‘for’ loop]
	[printing the transpose matrix]
	Print "The transpose of the matrix is:"
	For i=0 to n-1 repeat
		For j=0 to m-1 repeat
			Print t[i][j]
			If j=m-1 then
				Move to the next line
			[End of ‘if’]
		[End of ‘for’ loop]
	[End of ‘for’ loop]
Step 3: Stop.

Code

TIME COMPLEXITY:

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

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

        {     t[j][i]=a[i][j];

                }

                //printing the transpose matrix

                printf("\nThe transpose of the matrix is:\n");

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

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

                                {              printf("%d ",t[i][j]);

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

                                                                printf("\n");

                                }

The time complexity is O(m*n). if the number of rows and columns are equal, then the time complexity is O(n2).

 

APPLICATION:

  1. The transpose of a matrix is used to flip a matrix over its diagonal.

Contributed by