在一个方法中,我有一个 m*n 矩阵和一个数组(分配为 m*1 矩阵),不幸的是在编译过程中我收到此错误:
main.c:337:30: error: invalid operands to binary * (have ‘float’ and ‘float *’)
yp += matrixA[i][j] * listB[j];
该方法的完整代码是这样的
float error(float **matrixA, float **listB, int m, int n) {
int i, j;
float err = 0, de, yp;
for (i = 0; i < m; i++) {
yp = 0;
for (j = 0; j < n; j++)
yp += matrixA[i][j] * listB[j];
de = fabs(matrixA[i][j]-yp);
err += (de*de);
}
return err;
}
matrixA和listB都使用相同的分配方法。
为什么会出现这个错误? 我应该如何修复?
最佳答案
listB[j]
是 float *
类型。您将其与 float
类型相乘。改变
yp += matrixA[i][j] * listB[j];
到
yp += matrixA[i][j] * listB[j][i];
关于c - 当我尝试将两个矩阵中的元素相乘时,二进制操作数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24275293/