如何在 C++ 中使用特征库执行矩阵矩阵除法

How to perform matrix matrix division using eigen library in C++

我做了一个 MATLAB 代码,它必须执行

B3=abs(B2/max(B2));

其中 B2 是一个 181 x 238 矩阵,max(B2) 应该给我一个 1 x 238 的矩阵,其中包含每列中的最大值,并且 B3 应该是 181x1 矩阵。使用 Eigen 库的等效 C++ 代码应该是什么?请帮忙。 在修改我的代码时,使用更简单的维度比如 2 x 2 矩阵

//problem
#include <iostream>
#include<complex.h>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;
using Eigen::MatrixXd;

int main()
{
    MatrixXd A(2,2);MatrixXd B(2,1);MatrixXd C(1,2);
    A<<4,12,
       6,8;
            C=A.colwise().maxCoeff();
        //B=(A*(1.0/C)).cwiseAbs();
             B=A.array()/C.array();
   cout << "The solution is A :\n" << B.cwiseAbs()<< endl;

    return 0;
}

但是我无法执行这段代码。

hp@hp-HP-笔记本:~/beamforming/programs/eigen_prog$ g++ mm_t.cpp -o mm_t

hp@hp-HP-笔记本:~/beamforming/programs/eigen_prog$ ./mm_t mm_t: /usr/local/include/eigen3/Eigen/src/Core/CwiseBinaryOp.h:110: Eigen::CwiseBinaryOp::CwiseBinaryOp(const Lhs&, const Rhs&, const BinaryOp&) [with BinaryOp = Eigen::internal::scalar_quotient_op; LhsType = const Eigen::ArrayWrapper >; RhsType = const Eigen::ArrayWrapper >; Eigen::CwiseBinaryOp::Lhs = Eigen::ArrayWrapper >; Eigen::CwiseBinaryOp::Rhs = Eigen::ArrayWrapper >]:断言`aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()'失败的。 已中止(核心已转储)

知道哪里出了问题吗?? 我在我的 MATLAB 命令中做了简单的执行 window 以简化我想要作为输出获得的内容。

m=[4,12;6,8]

米=

 4    12
 6     8

max(m)

ans = 6 12

abs(m/max(m))

ans =

0.9333
0.7333

这个问题困扰我好久了。请帮助。

我对B3=abs(B2/max(B2))的解释如下。

  • b = max(B2)是包含B2.

  • 各列最大元素的行向量
  • q = B2/b表示超定线性方程q b = B2的最小二乘解。 (有nrow个独立问题,其中nrowB2的行数)。这个等式等价于 b^T q^T = B2^T,其中 ^T 是我的转置符号,我猜这种形式在许多库中更频繁地实现。

  • abs(q)表示q.

  • 的元素绝对值

所以,要求的结果是下面的x。也许。

#include <iostream>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;

int main()
{
  MatrixXd A(2,2), Atr(2,2);
  VectorXd b(2), x(2);
  A<<4,12,
    6,8;
  cout << "A :\n" << A << endl;

  Atr=A.transpose();
  cout << "Atr :\n" << Atr << endl;

  b=A.colwise().maxCoeff();
  cout << "b :\n" << b << endl;

  x = b.colPivHouseholderQr().solve(Atr).cwiseAbs();
  cout << "x :\n" << x << endl;

  return 0;
}

输出是

A :
 4 12
 6  8
Atr :
 4  6
12  8
b :
 6
12
x :
0.933333
0.733333

比照

https://eigen.tuxfamily.org/dox/group__LeastSquares.html


  • 以下是我基于对 Matlab 中 A/v 定义的误解的旧答案。

也许问题中的结果B3对应下面的向量x

#include <iostream>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;

int main()
{
  MatrixXd A(2,2);
  VectorXd b(2), x(2);
  A<<4,12,
    6,8;
  cout << "A :\n" << A << endl;

  b=A.colwise().maxCoeff();
  cout << "b :\n" << b << endl;

  x = A.colPivHouseholderQr().solve(b).cwiseAbs();
  cout << "x :\n" << x << endl;

  return 0;
}

cf

http://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html


以下是基于我对 matlab 中 max(A) 的误解的旧错误答案。

在Matlab中,max(A)是矩阵A的最大元素,abs(A)returns是取[=各元素绝对值的矩阵32=]。 所以,如果 B2 是特征矩阵对象,也许

B2=(B2*(1.0/B2.maxCoeff())).cwiseAbs()

比照。 https://www.mathworks.com/help/matlab/ref/abs.html?searchHighlight=abs&s_tid=gn_loc_drop http://eigen.tuxfamily.org/dox/group__QuickRefPage.html