所以我试图写一个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创建一个二维数组有关,但是,我不确定如何执行它。
如果您的矩阵是A
和B
,并且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] ...
所以:
您将要检查nColsOfA == nRowsOfB
,因为否则未定义乘法。
你会希望C
拥有nRowsofA
的行和nColsOfB
列。
您将需要在代码的内部循环内添加另一个循环,以计算每个的总和C[i][j]
。这会将值循环0
到nColsofA - 1
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句