_12_ Matrix __matrixmultiply

Source: Internet
Author: User

 Public classmatrixmultiply{ Public Static voidMain () {//declares a two-dimensional array of 2 rows and 3 columns to hold the matrix a        int[,] Matrixa =New int[2,3]; //declares a two-dimensional array of 3 rows and 4 columns to hold the matrix B//Initialize Matrix B        int[,] Matrixb =New int[3,4]{ {4,2,1,7}, {3,6,1,0}, {5,3,2,4} }; //declares a two-dimensional array of 2 rows and 4 columns that holds the product of matrix A and matrix B        int[,] Matrixc =New int[2,4]; //Initialize matrix A         for(inti =0; I <2; i++)        {             for(intj =0; J <3; J + +) {Matrixa[i,j]= (i +2) * (j +2) +1; }        }        //calculating the product of matrix A and matrix B         for(inti =0; I <2; i++)        {             for(intj =0; J <4; J + +)            {                //Initialize Matrix CMATRIXC[I,J] =0; //computes the product of matrix A and matrix B, and stores the value in Matrix C                 for(intK =0; K <3; k++) {Matrixc[i,j]+ = matrixa[i,k] *Matrixb[k,j]; }            }        }        //Print matrix AConsole.WriteLine ("\n******** Matrix a********"); Outputmatrix (Matrixa,2,3); //Print Matrix BConsole.WriteLine ("\n******** Matrix b********"); Outputmatrix (MATRIXB,3,4); //Print Matrix CConsole.WriteLine ("\n******** matrix A X matrix b********"); Outputmatrix (MATRIXC,2,4); }    //function: Output matrix//Parameters://MatrixX: The Matrix to be printed//rowCount: Number of rows in a matrix//columnCount: Number of columns in a matrix    Private Static voidOutputmatrix (int[,] MatrixX,intRowCount,intColumnCount) {        //if the number of rows is wrong, the matrix is not printed        if(Matrixx.length! = RowCount *ColumnCount) {Console.WriteLine ("The number of rows is wrong!"); return ; }                //Print Matrix         for(inti =0; i < RowCount; i++)        {            //Print all the elements in each row individually             for(intj =0; J < ColumnCount; J + +) {Console.Write (Matrixx[i,j]+"\ t"); }            //line BreakConsole.WriteLine (); }    }}

Operation Result:

_12_ Matrix __matrixmultiply

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.