c++ - 比较矩阵乘法

标签 c++ arrays matrix

我必须将一个矩阵与其自身相乘,直到该矩阵在某种程度上不等于前面的矩阵之一。然后我需要获取矩阵相等的度数值。行数和列数相等。矩阵存储在二维数组中。值为 0 或 1。检查与先前矩阵是否相等的最佳方法是什么?我尝试使用 vector 来存储矩阵:

vector<int[5][5]> m;

但我收到错误无法从“const int [5][5]”转换为“int [5][5]”

等待建议。

最佳答案

如果你可以使用boost ,看看升压Matrix类:

它似乎缺少 == 运算符,但很容易添加:

#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;
}

[Code]

关于c++ - 比较矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119694/

相关文章:

c++ - 迭代 vector 不会更新对象

c++ - 将 C++ new 运算符与函数一起使用的明智方法是什么?

c++ - 对象的销毁是否正确发生?

java - 生成介于 0 和 y 之间的随机数,x 是在 java 中生成的数量

c - 线程内读取错误的数组

python - 优化 numpy 矩阵运算(当前使用 for 循环)

java - 在java中创建一个矩阵

matrix - 如何从索引获取行和列?

c++ - 如何访问 PCLPointCloud2 类型的点

javascript - 已知关联元素时如何从 JavaScript 对象获取值?