How can I multiply two matrices in C#?

后端 未结 10 1879
不思量自难忘°
不思量自难忘° 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:01

    I wrote a small program to multiply two 3 x 3 matrices, as part of my neural network for my A-level project. Hope people find it useful.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _3_x_3_Matrix_multiplier
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,,] matrix = new int[3, 3, 3];
                for (int z = 0; z < 2; z++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        for (int x = 0; x < 3; x++)
                        {
                            Console.WriteLine("element: {0} , {1}", x, y);
                            matrix[x, y, z] = int.Parse(Console.ReadLine());
                        }
                    }
                }
                for (int xm = 0; xm < 3; xm++)
                {
                    for (int ym = 0; ym < 3; ym++)
                    {
                        for (int zm = 0; zm < 3; zm++)
                        {
                            matrix[xm, ym, 2] += (matrix[0 + zm, ym, 0] * matrix[xm, 0 + zm, 1]);
                        }
                    }
                }
                for (int i = 0; i < 3; i++)
                {
                    Console.Write("\n");
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(matrix[j, i, 2] + " ");
                    }
                }
                Console.ReadLine();
            }
        }
    }
    

提交回复
热议问题