2014-12-03 72 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