C#完成矩陣乘法實例剖析。本站提示廣大學習愛好者:(C#完成矩陣乘法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成矩陣乘法實例剖析正文
本文實例講述了C#完成矩陣乘法的辦法。分享給年夜家供年夜家參考。詳細以下:
static double[][] MatrixMultiplication(double[][] matrixOne, double[][] matrixTwo)
{
int aRows = matrixOne.Length; int aCols = matrixOne[0].Length;
int bRows = matrixTwo.Length; 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;
}
願望本文所述對年夜家的C#法式設計有所贊助。