2014-12-03 87 views
-2

所以我想寫一個C函數來計算兩個數組的乘法,但我有點卡住了。矩陣乘法問題

double ** matrixMultiply(double **A, int nRowsOfA, int nColsOfA, 
double **B, int nRowsOfB, int nColsOfB) 
{ 

    double **out; 
    int i, j, l; 

    out=(double **)malloc(nRowsOfA*sizeof(double *)); 
    for (i=0;i<nRowsOfA; i++) 
     out[i]=(double *)malloc(nColsOfB*sizeof(double)); 



    for (i=0;i<nRowsOfA; i++) 
     for (j=0; j<nColsOfB; j++) 
     { 
      Some calculation to figure out how to multiply the two matrices together. 
     } 
    return out; 

} 

我敢肯定它有事情做與i和j創建一個2-d陣列,但是,是的,我不知道如何執行它。

+1

你能解釋一下你面臨什麼問題嗎 – akashchandrakar 2014-12-03 05:00:57

+0

請更專注於哪些方法無效。 – Codor 2014-12-03 07:51:31

回答

0

如果你的矩陣是ABAB = C,進入C[i][j]由下式給出:

A[i][1] x B[1][j] + A[i][2] x B[2][j] + A[i][3] x B[3][j] ... 

所以:

  1. 您將要檢查nColsOfA == nRowsOfB,否則乘法沒有定義。

  2. 你會想要CnRowsofA行和nColsOfB列。

  3. 你會想要在你的代碼中的內部循環內的另一個循環來計算每個C[i][j]以上的總和。這將循環使用值0nColsofA - 1