我的矩阵乘法代码什么都不做(C++)
My matrix multiplication code does nothing (C++)
我的矩阵乘法函数模板如下:
template<typename element_type>
void matMul(const std::vector<std::vector<element_type>> & mat1,
const std::vector<std::vector<element_type>> & mat2,
std::vector<std::vector<element_type>> & result
) {
if (mat1[0].size() != mat2.size()) {
std::cout << "dimensions do not match..." << std::endl;
return;
}
result.resize(mat1.size());
for (std::vector<double> & row : result) {
row.resize(mat2[0].size());
}
for (unsigned int row_id = 0; row_id < mat1.size(); ++row_id) {
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
for (unsigned int element_id = 0; element_id < mat1[0].size(); ++element_id) {
////////////////////////////////////////////////////////////////////////////////////
result[row_id][col_id] += mat1[row_id][element_id] * mat2[element_id][col_id];//HERE I WILL MENTION BELOW...
////////////////////////////////////////////////////////////////////////////////////
}
}
}
我通过了
std::vector<std::vector<double>> mul1 = {
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}
};
,
std::vector<std::vector<double>> mul2 = {
{7.0, 8.0},
{9.0, 10.0},
{11.0, 12.0}
};
和
std::vector<std::vector<double>> result;
下一个代码用于测试:
matMul(mul1, mul2, result);
for (std::vector<double> row : result) {
for (double element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
输出为:
0 0
0 0
我在Visual Studio2017年调试的时候,发现断点在我上面提到的地方不起作用。它似乎什么都不做,只是绕过了那部分。为什么我的 VS2017 会忽略该部分?以及如何修复它?
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
检查你的终止条件。这似乎不对。你的意思是:
for (unsigned int col_id = 0; col_id < mat2[0].size(); ++col_id) {
我的矩阵乘法函数模板如下:
template<typename element_type>
void matMul(const std::vector<std::vector<element_type>> & mat1,
const std::vector<std::vector<element_type>> & mat2,
std::vector<std::vector<element_type>> & result
) {
if (mat1[0].size() != mat2.size()) {
std::cout << "dimensions do not match..." << std::endl;
return;
}
result.resize(mat1.size());
for (std::vector<double> & row : result) {
row.resize(mat2[0].size());
}
for (unsigned int row_id = 0; row_id < mat1.size(); ++row_id) {
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
for (unsigned int element_id = 0; element_id < mat1[0].size(); ++element_id) {
////////////////////////////////////////////////////////////////////////////////////
result[row_id][col_id] += mat1[row_id][element_id] * mat2[element_id][col_id];//HERE I WILL MENTION BELOW...
////////////////////////////////////////////////////////////////////////////////////
}
}
}
我通过了
std::vector<std::vector<double>> mul1 = {
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}
};
,
std::vector<std::vector<double>> mul2 = {
{7.0, 8.0},
{9.0, 10.0},
{11.0, 12.0}
};
和
std::vector<std::vector<double>> result;
下一个代码用于测试:
matMul(mul1, mul2, result);
for (std::vector<double> row : result) {
for (double element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
输出为:
0 0
0 0
我在Visual Studio2017年调试的时候,发现断点在我上面提到的地方不起作用。它似乎什么都不做,只是绕过了那部分。为什么我的 VS2017 会忽略该部分?以及如何修复它?
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
检查你的终止条件。这似乎不对。你的意思是:
for (unsigned int col_id = 0; col_id < mat2[0].size(); ++col_id) {