SoFunction
Updated on 2025-03-07

C# implements matrix multiplication example analysis

This article describes the method of implementing matrix multiplication in C#. Share it for your reference. The details are as follows:

static double[][] MatrixMultiplication(double[][] matrixOne, double[][] matrixTwo)
{
 int aRows = ; int aCols = matrixOne[0].Length;
 int bRows = ; int bCols = matrixTwo[0].Length;
 if (aCols != bRows)
 throw new Exception("Out of shape matrices");
 double[][] result = CreateMatrix(aRows, bCols);
 for (int i = 0; i < aRows; ++i) // each row of MatrixOne
 for (int j = 0; j < bCols; ++j) // each col of MatrixTwo
  for (int k = 0; k < aCols; ++k)
 result[i][j] += matrixOne[i][k] * matrixTwo[k][j];
 return result;
}
static double[][] CreateMatrix(int rows, int cols)
{
 double[][] result = new double[rows][];
 for (int i = 0; i < rows; ++i)
 result[i] = new double[cols]; 
 return result;
}

I hope this article will be helpful to everyone's C# programming.