簡體   English   中英

將兩個矩陣相乘

[英]Multiply two matrices

我需要協助。 我必須編寫一個將兩個矩陣相乘的方法。 這是我的代碼,但是我得到了IndexOutOfRangeException

static int[,] FalkScheme(
    int[,] Matrix1,
    int[,] Matrix2,
    int rowcountmatrix1,
    int columncountmatrix1,
    int rowcountmatrix2,
    int columncountmatrix2)
{
    int[,] NewMatrix = new int[rowcountmatrix1, columncountmatrix2];

    if (columncountmatrix1 != rowcountmatrix2)
    {
        throw new Exception("the dimensions of the matrices do not fit! Please try operation with other matrices again!");            
    }
    else
    {
        for (int i = 0; i < rowcountmatrix1; i++)
        {
            for (int u = 0; u < columncountmatrix2; u++)
            {
                NewMatrix[i, u] = Matrix1[i, u] * Matrix2[i, u] + Matrix1[i, u + 1] * Matrix2[i + 1, u] + Matrix1[i, u + 2] * Matrix2[i + 2, u];
            }
        }

        return NewMatrix;
    }
}

您能幫我解決這個問題嗎?

您不需要任何rowcountmatrix1 .. columncountmatrix2 :詢問數組的尺寸

public static int [,] FalkScheme(int[,] Matrix1, int[,] Matrix2) {
  if (Object.ReferenceEquals(null, Matrix1))
    throw new ArgumentNullException("Matrix1");
  else if (Object.ReferenceEquals(null, Matrix2))
    throw new ArgumentNullException("Matrix2");

  int r1 = Matrix1.GetLength(0);
  int c1 = Matrix1.GetLength(1);

  int r2 = Matrix2.GetLength(0);
  int c2 = Matrix2.GetLength(1);

  if (c1 != r2)
    throw new ArgumentOutOfRangeException("Matrix2", "Matrixes dimensions don't fit.");

  int[,] result = new int[r1, c2];

  // Naive matrix multiplication: O(n**3) 
  // Use Strassen algorithm O(n**2.81) in case of big matrices
  for (int r = 0; r < r1; ++r) 
    for (int c = 0; c < c2; ++c) {
      int s = 0;

      for (int z = 0; z < c1; ++z) 
        s += Matrix1[r, z] * Matrix2[z, c];

      result[r, c] = s;
    }

  return result;
}

測試

   int[,] a = new int[,] { {1, 2, 3}, {4, 5, 6}};
   int[,] b = new int[,] { { 5, 6 }, { 7, 8 } , {9, 10}};
   //  46  52
   // 109 124 
   int[,] c = Problems.FalkScheme(a, b);

在一個循環中,您使用i或u進行計數,並包含代碼i+1u+1 ,這使您超出了限制-假設rowcount是數組中的行數,例如10,i上一次迭代中的+1將為10,並且由於數組被索引為0,因此從第0個元素開始就沒有第10個元素

我可以看到您將收到“超出范圍”異常。

假設矩陣為5行5列,例如,兩個矩陣中的最低索引為0,0,最高索引為4,4。 在循環中,您從0,0到4,4,因此當您位於4,4時,您會尋找“ Matrix2 [i,u + 2]”,即4、6。它高於4,4,因此OutOfRangeException。

您需要以下內容:

for (int i = 0; i < rowcountmatrix1; i++)
{
    for (int u = 0; u < columncountmatrix2; u++)
    {
        NewMatrix[i, u] = 0; // The position in the new Matrix we need to calculate

        for (int x = 0; x < rowcountmatrix2 /* same as columncountmatrix1 */; x++)
        {
            NewMatrix[i, u] += Matrix1[i, x] * Matrix2[x, u];
        }
    }
}

你有:

Matrix1[i, u] * Matrix2[i, u] + Matrix1[i, u + 1] * Matrix2[i + 1, u] + Matrix1[i, u + 2] * Matrix2[i + 2, u]

(三個詞)。 您的方法是否打算僅分別乘以大小為m×33×n矩陣? 否則,將這三個項的總和更改為columncountmatrix1== rowcountmatrix2 )項的總和。

考慮類似

Enumerable.Range(0, columncountmatrix1).Sum(x => Matrix1[i, x] * Matrix2[x, u]);

正如其他人所說,這兩個矩陣的大小內置在數組對象中,因此您的方法不應將它們作為單獨的參數使用。

實際上,您需要三個for循環,請嘗試以下代碼。

for (int i = 0; i < N; i++)
{
    for (int j = 0; j < M; j++) 
    {
        C[i,j] = 0;
        for (int k = 0; k < M; k++)
        {
            C[i,j] += A[i,k] * B[k,j];
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM