How can I multiply two matrices in C#?

后端 未结 10 1795
不思量自难忘°
不思量自难忘° 2021-01-02 03:24

Like described in the title, is there some library in the Microsoft framework which allows to multiply two matrices or do I have to write my own method to do this? // I\'ve

10条回答
  •  长发绾君心
    2021-01-02 04:10

    Below is the method to multiply int[3,4] matrix with int[4,3] matrix, it has time complexity of O(n cube) or Cubic time

    class Program { static void Main(string[] args) {

            MultiplyMatrix();
        }
    
        static void MultiplyMatrix()
        {
            int[,] metrix1 = new int[3,4] { { 1, 2,3,2 }, { 3, 4,5,6 }, { 5, 6,8,4 } };
            int[,] metrix2 = new int[4, 3] { { 2, 5, 3 }, { 4, 5, 1 }, { 8, 7, 9 }, { 3, 7, 2 } };
            int[,] metrixMultplied = new int[3, 3];
    
            for (int row = 0; row < 3; row++)
            {
                for (int col = 0; col < 3; col++)
                { 
                    for(int i=0;i<4;i++)
                    {
                        metrixMultplied[row, col] = metrixMultplied[row, col] + metrix1[row, i] * metrix2[i, col];
    
                    }
                    Console.Write(metrixMultplied[row, col] + ", ");                   
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
    

提交回复
热议问题