The work used in the row and column transpose, the two cases of the algorithm to write down, so that after the use of
1. Transpose with equal number of rows
1 /**2 * @description Matrix transpose3 * @authorOldmonk4 * @time August 18, 20175 */6 Public classTest {7 8 Public Static voidmain (String [] args) {9 intdata [] =New int[] [] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9 } } ;TenSystem.out.println ("----------------Transpose before------------------------") ; One print1 (data); A reverse (data); -System.out.println ("----------------------------------------after Transpose") ; - print1 (data); the } - - //Transpose a matrix - Public Static voidReverseintTemp []) { + for(inti = 0; i < temp.length; i++) { - for(intj = i; J < Temp[i].length; J + +) { + intK =Temp[i][j]; ATEMP[I][J] =Temp[j][i]; atTemp[j][i] =K; - } - } - } - - //The matrix output in Public Static voidPrint1 (intTemp []) { - for(inti = 0; i < temp.length; i++) { to for(intj = 0; J < Temp[i].length; J + +) { +System.out.print (Temp[i][j] + "\ T") ; - } the System.out.println (); * } $ }Panax Notoginseng}
Test Results:
2. Arbitrary array transpose
1 /**2 * @description arbitrary array transpose3 * @authorOldmonk4 * @time August 18, 20175 */6 Public classTest2 {7 8 Public Static voidMain (String [] args)//Test9 {Ten Double[] Testmatrix = {{1, 22, 34, 22}, {1, 11, 5, 21}, {7, 2, 13, 19 } } ; One Double[] Matrixc = Transpose (Testmatrix, 3, 4) ; A -System.out.println ("-------Transpose before---------") ; - Myprint (Testmatrix); theSystem.out.println ("----------------after Transpose") ; - Myprint (MATRIXC); - } - + /** - * @descript arbitrary two-dimensional array transpose + * @authorXujingyang A * @time August 17, 2017 at */ - Public Static Double[] Transpose (Double[] Matrix,intLine,intList) { - Double[] Matrixc =New Double[List] [line]; - for(inti = 0; I < line; i++) { - for(intj = 0; J < List; J + +) { -Matrixc[j][i] =Matrix[i][j]; in } - } to returnMatrixc; + } - the //The matrix output * Public Static voidMyprint (DoubleTemp []) { $ for(inti = 0; i < temp.length; i++) {Panax Notoginseng for(intj = 0; J < Temp[i].length; J + +) { -System.out.print (Temp[i][j] + "\ T") ; the } + System.out.println (); A } the } +}
Test Results:
Java Matrix transpose algorithm