我必须将一个矩阵与其自身相乘,直到该矩阵在某种程度上不等于前面的矩阵之一。然后我需要获取矩阵相等的度数值。行数和列数相等。矩阵存储在二维数组中。值为 0 或 1。检查与先前矩阵是否相等的最佳方法是什么?我尝试使用 vector
来存储矩阵:
vector<int[5][5]> m;
但我收到错误无法从“const int [5][5]”转换为“int [5][5]”
。
等待建议。
最佳答案
它似乎缺少 ==
运算符,但很容易添加:
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
template<typename T>
bool operator==(const matrix<T>& m, const matrix<T>& n)
{
bool returnValue =
(m.size1() == n.size1()) &&
(m.size2() == n.size2());
if (returnValue)
{
for (unsigned int i = 0; returnValue && i < m.size1(); ++i)
{
for (unsigned int j = 0; returnValue && j < m.size2(); ++j)
{
returnValue &= m(i,j) == n(i,j);
}
}
}
return returnValue;
}
像这样使用:
int main ()
{
matrix<double> m (3, 3);
for (unsigned int i = 0; i < m.size1(); ++ i)
{
for (unsigned int j = 0; j < m.size2(); ++ j)
{
m (i, j) = 3 * i + j;
}
}
std::cout << m << std::endl;
matrix<double> n (3, 3);
std::cout << (m == n) << std::endl;
std::cout << (m == m) << std::endl;
}
关于c++ - 比较矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119694/