【发布时间】:2021-10-17 18:26:41
【问题描述】:
我遇到的问题是如何为 K 的最内层循环获取正确的数字列。 一个例子是一个 2x3 矩阵和一个 3x2 矩阵相乘。 结果应该是一个 2x2 矩阵,但目前我不知道如何将 2 的值发送给运算符重载函数。 它应该是 诠释 k = 0; k
Matrix::Matrix(int row, int col)
{
rows = row;
cols = col;
cx = (float**)malloc(rows * sizeof(float*)); //initialize pointer to pointer matrix
for (int i = 0; i < rows; i++)
*(cx + i) = (float*)malloc(cols * sizeof(float));
}
Matrix Matrix::operator * (Matrix dx)
{
Matrix mult(rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
mult.cx[i][j] = 0;
for (int k = 0; k < ?;k++) //?????????????
{
mult.cx[i][j] += cx[i][k] * dx.cx[k][j];
}
}
}
mult.print();
return mult;
//calling
Matrix mult(rowA, colB);
mult = mat1 * mat2;
}
【问题讨论】:
-
请不要在一个问题中添加多个语言标签。仅使用您正在使用的语言的标签。
-
很抱歉,这很无聊,但要让矩阵类正常工作需要一段时间。使用 BLAS,它是 Boost 发行版的一部分。
-
cx = (float**)malloc(rows * sizeof(float*));是 C++ 中的未定义行为你为什么不使用std::vector?
标签: c++ matrix operator-overloading