transpose of a matrix in java without using second matrix

Solutions on MaxInterview for transpose of a matrix in java without using second matrix by the best coders in the world

showing results for - "transpose of a matrix in java without using second matrix"
Curtis
01 Jul 2017
1import java.util.Scanner;
2public class MatrixTransposeInJava
3{
4   public static void main(String[] args)
5   {
6      int[][] arrGiven = {{2,4,6},{8,1,3},{5,7,9}};	    
7      // another matrix to store transpose of matrix  
8      int[][] arrTranspose = new int[3][3];	    
9      // transpose matrix code  
10      for(int a = 0; a < 3; a++)
11      {    
12         for(int b = 0; b < 3; b++)
13         {    
14            arrTranspose[a][b] = arrGiven[b][a];  
15         } 
16      }	  
17      System.out.println("Before matrix transpose: ");  
18      for(int a = 0; a < 3; a++)
19      {    
20         for(int b = 0; b < 3; b++)
21         {    
22            System.out.print(arrGiven[a][b] + " ");    
23         } 
24         System.out.println();
25      }    
26      System.out.println("After matrix transpose: ");  
27      for(int a = 0; a < 3; a++)
28      {    
29         for(int b = 0; b < 3; b++)
30         {    
31            System.out.print(arrTranspose[a][b] + " ");    
32         } 
33         System.out.println();
34      }
35   }
36}
Juan Manuel
30 Feb 2020
1// transpose of a matrix in java without using second matrix
2import java.util.Scanner;
3public class WithoutSecondMatrix
4{
5   public static void main(String[] args)
6   {
7      Scanner sc = new Scanner(System.in);
8      int a, b, row, column, temp;
9      System.out.println("Please enter number of rows: ");
10      row = sc.nextInt();
11      System.out.println("Please enter the number of columns: ");
12      column = sc.nextInt();
13      int[][] matrix = new int[row][column];
14      System.out.println("Please enter elements of matrix: ");
15      for(a = 0; a < row; a++)
16      {
17         for(b = 0; b < column; b++)
18         {
19            matrix[a][b] = sc.nextInt();
20         }
21      }
22      System.out.println("Elements of the matrix: ");
23      for(a = 0; a < row; a++)
24      {
25         for(b = 0; b < column; b++)
26         {
27            System.out.print(matrix[a][b] + "\t");
28         }
29         System.out.println("");
30      }
31      // transpose of a matrix
32      for(a = 0; a < row; a++)
33      {
34         for(b = 0; b < a; b++)
35         {
36            temp = matrix[a][b];
37            matrix[a][b] = matrix[b][a];
38            matrix[b][a] = temp;
39         }
40      }
41      System.out.println("transpose of a matrix without using second matrix: ");
42      for(a = 0; a < row; a++)
43      {
44         for(b = 0; b < column; b++)
45         {
46            System.out.print(matrix[a][b] + "\t");
47         }
48         System.out.println("");
49      }
50      sc.close();
51   }
52}
Melina
03 Nov 2018
1for (int row = 0; row < matrix.length; row++) {
2    
3   for (int col = row; col < matrix[row].length; col++) 
4   {
5    	// Swap 
6     	int data = matrix[row][col];
7    	matrix[row][col] = matrix[col][row];
8   		matrix[col][row] = data;
9   }
10  
11  }
queries leading to this page
java program to find transpose of a matrixtranspose of a matrix javatranspose of a matrix in javatranspose matrix in place javaprint transpose of a matrix in javafind transpose of a matrix in javareturn a two dimensional matrix of integers obtained from multiplication of all increment matrix and its transposejava function to transpose a matrixmatrix transpose codetransposing a matrix in javahow to find transpose of matrix in javajava transpose matrixtranspose function of a matrix in javatranspose of a matrix in ajvadisplay the transpose of a given matrix javajava transpose one object to anothertranspose of a 3x3 matrix in javaprogram to transpose a matrix in javawrite a program to find the transpose of a given matrix in javatranspose a matrix javatranspose of matrix in java inplacetranspose of a matrixin javatranspose matrix javatranspose a matrix in javatranspose in javatranspose matrix in javajava matrix transposetransposing a 2d array math vector in javatranspose a matrix c 2b 2btranspose matrix java program in java in a single matrixjava how to transpose a matrixmatrix transpose in javatranspose codetranspose of a matrix code in javawrite pseudo code for input matrix to compact form 2c simple transpose and fast transposereverse a matrix in javatranspose of a matrix in java without using second matrixhow to do transpose of matrix javatranspose of a matrix program in javafind transpose of a matrix javatranspose of matrix in java using functiontranspose of a given matrix javawrite a program for finding the transpose of a 3 x3 matrix in javatranspose of a square matrix coding blockstrnaspose matrix javajava program to transpose a matrixhow to transpose a matrix in javajava program to find the transpose of a given matrix trampose matix in javajava code to get matrix transposetranspose of a matrix using rmi in javatranspose of matrix javahow to create a transpose of a matrix javatranspose matrix code javatransponierte matrix javajava program to find the transpose of a given matrixjava code to transpose a matrixmatrix transpose java codefunction to transpose a matrix in javawrite a java program to perform transpose of a matrixtranspose matrix program in java without using another matrixmatrix transpose javajava program for transpose matrixtranspose of matrix in javatranspose of a matrix in java without using second matrix