矩阵乘法问题

唐纳德·当

所以我试图写一个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创建一个二维数组有关,但是,我不确定如何执行它。

托尼

如果您的矩阵是AB,并且AB = 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. 你会希望C拥有nRowsofA的行和nColsOfB列。

  3. 您将需要在代码的内部循环内添加另一个循环,以计算每个的总和C[i][j]这会将值循环0nColsofA - 1

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章