Java Simple matrix multiplication operation

Source: Internet
Author: User

1. Calculated two matrices where the number of columns in the first matrix must be the same as the number of rows in the second matrix;

2. The number of rows in the first matrix determines the number of rows in the result matrix, and the number of columns in the second matrix determines the number of columns in the result matrix;

1  PackageOrg.admln.matrix;2 /*3 * 201502114  */5  Public classmatrixmultiplication {6      Public Static voidMain (string[] args) {7         /*8 * 1. Build the Matrix9 * 2 9 7 4 2Ten * matrix1= 3 1 4 matrix2= 6 8 One * 2 6 A          */ -         int[] matrix1 = {{2,9,7},{3,1,4}};  -         int[] matrix2 = {{4,2},{6,8},{2,6}};  theSystem.out.println ("matrix1 matrix is" + Matrix1.length + "Row" + matrix1[0].length + "column"); -          for(inti=0;i<matrix1.length;i++) { -              for(intj=0;j<matrix1[i].length;j++) { -System.out.print ("\ T" +matrix1[i][j]); +             } - System.out.println (); +         } ASystem.out.println ("matrix2 matrix is" + Matrix2.length + "Row" + matrix2[0].length + "column"); at          for(inti=0;i<matrix2.length;i++) { -              for(intj=0;j<matrix2[i].length;j++) { -System.out.print ("\ T" +matrix2[i][j]); -             } - System.out.println (); -         } in         /* - * 2. Multiply matrix to          */ +SYSTEM.OUT.PRINTLN ("Result matrix is" + Matrix1.length + "Row" + matrix2[0].length + "column"); -          for(inti=0;i<matrix1.length;i++) { the              for(intj=0;j<matrix2[0].length;j++) { *                 intsum = 0; $                  for(intk=0;k<matrix2.length;k++) {Panax NotoginsengSum + = matrix1[i][k] *Matrix2[k][j]; -                 } theSystem.out.print ("\ T" +sum); +             } A System.out.println (); the         } +     } -}

Java Simple matrix multiplication operation

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.