Menu
Topics Index
...
`

Find The Transpose Of Matrix


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to get the transpose of the given matrix.

Input (Two Dimensional matrix) Output (Two Dimensional matrix)
21 23 34 45 37
11 19 13 17 15
 9  6  7  5  1
101 99 77 33 121
25 56 44 22 66
21  11  9  101  25
23  19  6  99  56
34  13  7  77  44
45  17  5  33  22
37  15  1  121  66
10   2   5
7   13   9
6   8   11
10   7   6
2   13   9
5   9   11
99 101 111 121
10 66 77 89
133 144 168 177
99 10 133
101 66 144
111 77 168
121 89 177
12   23   45
89   99   100
12   89
23   99
45   100



class FindTransposeOfMatrix
{
    public static void main(String s[])
    {
        int input[][] = {{10, 2, 5}, {7, 13, 9}, {6, 8, 11}};
        int output[][] = getTransposeOfMatrix(input);
        System.out.println("The transpose matrix is : ");
        for (int[] array1d : output) {
        for (int element : array1d) {
        System.out.print(element + " ");
        }
        System.out.println();
        }

    }


    public static int[][] getTransposeOfMatrix(int matrix[][]) {
    }

}
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

According to the program you need to have nested for loops (one for representing row and one for column) then copy 'j'th row 'i'th column value of input matrix to 'i'th row 'j'th column of output matrix...

Posted by Gopinath Manchikanti    2015-01-22 06:36:40


here we need to create another array to store the transport of the given array..in matrix transport rows become col and col become row so new Array dimensions are opposite to the old one..
new int[matrix[0].length][matrix.length]
new Array rows=original array cols
new Array cols=original array rows

now we need to copy the original array elements into the new array we can copt using the 2 for loops..

for(int i=0;i<matrix[0].length;i++)
for(int j=0;j<matrix.length;j++)
a[i][j]=matrix[j][i];
original array col elements must be inserted into the new array rows..

Posted by Uday Kumar    2015-01-22 06:38:33


Here Execution starts from main() method here we are creating an array variable and initializing with values {10,2,5}{7,13,9}{6,8,11} respectively... it is 3*3 matrix means 3-rows and 3-coloums after that we are creation array variable output initializing by calling the method getTransposeOfMatrix() to transpose matrix                                                          1.here we are passing input array as a parameter inside this method we have to take one variable to store the transposed matrix with range 3*3 i.e transp[3][3] after that we will take one for-loop with the range c=0 to c<3 and c++this is for rows inside this loop we have to take another for-loop with range d=0 to d<3 and d++ this is for coloumns inside this for-loop  we have to write one stmt                              transpose[d][c] = matrix[c][d] here c-is rows and d- is for coloumns so we are reversing those variables in transpose[d][c] varaible so it will replace rows values into coloumns after that we have to write one return stmt return transpose; then compiler will return to main() method and there we are taking 2 enhanced for loops one for rows and one for coloums and inside this for-loop we are printing those transposed values like this we are printing the transposed values in the form of matrix

Posted by Bhagi Bhagyasri    2015-01-22 06:56:37


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Second Correct Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-23 05:51:14


© meritcampus 2019

All Rights Reserved.

Open In App