C# 求两个距阵相乘的结果

求两个距阵相乘的结果:距阵乘法法则如下仅当一个m×n矩阵A的列数与另一个q×p矩阵B的行数相同时(即n = q),才可以执行矩阵乘法A*B。A*B所得到的m×p矩阵C满足以下关系:

图片说明

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3_两个矩阵相乘_二维数组_
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[3, 3] { { 1, 2, 3 }, { 9, 5, 7 }, { 12, -1, 0 } }; //定义矩阵a
            int[,] b = { { 8, 1, 1 }, { 0, 1, 1 }, { 0, -4, 1 } };  //定义矩阵b
            int[,] c = new int[3, 3];  //动态定义矩阵c
            Console.WriteLine("数组a为:");
            for (int i = 0; i < 3; i++)  //输出矩阵a
            { 
                for(int j = 0; j < 3; j++)
                {
                    Console.Write("{0, 5:d}", a[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("数组b为:");
            for (int i = 0; i < 3; i++)  //输出矩阵b
            { 
                for (int j = 0; j < 3; j++)
                {
                    Console.Write("{0, 5:d}", b[i, j]);
                }
                Console.WriteLine();
            }
            for (int i = 0; i < 3; i++)  //进行矩阵乘法
            {
                for (int j = 0; j < 3; j++) {
                    for(int k = 0; k < 3; k++)
                    {
                        c[i, j] += a[i, k] * b[k, j];
                    }
                }
            }
            Console.WriteLine("矩阵a*b后为:");
            for(int i = 0; i < 3; i++)  //输出矩阵c
            {
                for(int j = 0; j < 3; j++)
                {
                    Console.Write("{0 ,5:d}",c[i,j]);
                }
                Console.WriteLine();
            }
        }
    }
}

https://blog.csdn.net/summoxj/article/details/79938299