java program to transpose matrix

transpose matrix

The transpose of a matrix is a new matrix whose rows are the columns of the original. (This makes the columns of the new matrix the rows of the original). Here is a matrix and its transpose:
A new matrix whose rows are the columns of the original matrix is called Transpose of a matrix.

For example

// This java program find transpose of a matrix of any order.
public class Transpose
{
    public static void main(String args[])
    {
        int n[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int m[][]=new int[4][3];
        int i,j,k=0;
        System.out.println("Original");
        for(i=0;i<3;i++)
        {
            for(j=0;j<4;j++)
            {
                System.out.print(n[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("Tranpose");
        for(i=0;i<3;i++)
        {
            for(j=0;j<4;j++)
            {
                m[j][i]=n[i][j];
            }
            //System.out.println();
        }
        for(i=0;i<4;i++)
        {
         
            for(j=0;j<3;j++)
            {
               System.out.print(m[i][j]+" ");
            }
            k++;
            System.out.println();
        }
    }
}

Output
Original
1 2 3 4
5 6 7 8
9 10 11 12
Tranpose
1 5 9
2 6 10
3 7 11
4 8 12 

You may also like these program


SHARE THIS
Previous Post
Next Post