【问题标题】:How to perform matrix matrix division using eigen library in C++如何在 C++ 中使用 eigen 库执行矩阵矩阵除法
【发布时间】:2018-01-14 06:25:59
【问题描述】:

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

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

其中B2181 x 238 矩阵,max(B2) 应该给我一个1 x 238 的矩阵,其中包含每列中的最大值,B3 应该是181x1matrix。使用 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-Notebook:~/beamforming/programs/eigen_prog$ g++ mm_t.cpp -o mm_t

hp@hp-HP-Notebook:~/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::标量商操作; LhsType = const Eigen::ArrayWrapper >; RhsType = const Eigen::ArrayWrapper >;特征::CwiseBinaryOp::Lhs = 特征::ArrayWrapper >; Eigen::CwiseBinaryOp::Rhs = Eigen::ArrayWrapper >]: 断言 `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' 失败。 中止(核心转储)

知道有什么问题吗? 我在我的 MATLAB 命令窗口中进行了简单的执行,以简化我想要得到的输出。

m=[4,12;6,8]

m =

 4    12
 6     8

最大(米)

ans = 6 12

abs(m/max(m))

ans =

0.9333
0.7333

我被这个问题困扰了很长时间。请帮忙。

【问题讨论】:

  • maxCoeff() 返回整个矩阵中的最大元素。但是在 MATLAB max(A) 中,其中 A 是矩阵,应该给出包含每列最大值的行向量。我对我的问题进行了更改,更清楚地表明了我的矩阵尺寸
  • A * C.asDiagonal().inverse() ?其实我不知道你的abs是什么意思,所以这只是一个随机的建议,可能是错误的。
  • Y = abs( X ) 返回数组 X 中每个元素的绝对值。如果 X 是复数,abs(X) 返回复数幅度。不,这仍然会导致 2 x 2 矩阵 0.666667 1 1 0.666667
  • 那是你的划分混乱了,你真的应该解释一下每个操作的含义......

标签: c++ matlab eigen3


【解决方案1】:

我将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;
}

参考

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


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

在 Matlab 中,max(A) 是矩阵A 的最大元素,abs(A) 返回一个矩阵,该矩阵采用A 的各个元素的绝对值。 所以,如果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

【讨论】:

  • 谢谢。但是 M = max( A ) 返回 A 的最大元素。如果 A 是向量,则 max(A) 返回 A 的最大元素。如果 A 是矩阵,则 max(A) 是包含每列最大值的行向量。 maxCoeff() 仅返回矩阵的最大元素。那么如何修改上面的代码呢??
  • 你是对的。在 A 是矩阵的情况下,我对 max(A) 的看法是错误的。那么,Matlab中矩阵A和向量v是如何定义A/v的呢?
  • 啊,好吧。 A/v 表示xA x = v 的解决方案。
  • 啊,A/v 实际上是x,所以x A = v(不是A x = v)。我的答案还是错了。
  • 哦,不。 A/v 实际上是指xx v = A 的最小二乘解。这很奇怪,但无论如何我的答案仍然是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多