Java Matrix transpose algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.