简体   繁体   English

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

[英]How to perform matrix matrix division using eigen library in C++

I did a MATLAB code and it had to perform 我做了一个MATLAB代码,它必须执行

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

where B2 is an 181 x 238 matrix , max(B2) should give me a matrix of 1 x 238 comprising of maximum value in each column and B3 should be 181x1 matrix. 其中B2181 x 238矩阵, max(B2)应该给我一个1 x 238的矩阵,包括每一列的最大值,而B3应该是181x1矩阵。 What should be the equivalent C++ code using Eigen library? 使用本征库的等效C++代码应该是什么? Please help. 请帮忙。 On modifying my code, with simpler dimension say with 2 x 2 matrix 关于修改我的代码,用更简单的尺寸说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;
}

But I am not able to execute this code. 但是我无法执行此代码。

hp@hp-HP-Notebook:~/beamforming/programs/eigen_prog$ g++ mm_t.cpp -o mm_t 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::scalar_quotient_op; 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 :: scalar_quotient_op; LhsType = const Eigen::ArrayWrapper >; LhsType = const Eigen :: ArrayWrapper>; RhsType = const Eigen::ArrayWrapper >; RhsType = const Eigen :: ArrayWrapper>; Eigen::CwiseBinaryOp::Lhs = Eigen::ArrayWrapper >; Eigen :: CwiseBinaryOp :: Lhs = Eigen :: ArrayWrapper>; Eigen::CwiseBinaryOp::Rhs = Eigen::ArrayWrapper >]: Assertion `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed. Eigen :: CwiseBinaryOp :: Rhs = Eigen :: ArrayWrapper>]:断言`aLhs.rows()== aRhs.rows()&& aLhs.cols()== aRhs.cols()'失败。 Aborted (core dumped) 中止(核心已弃用)

Any idea what is wrong?? 知道有什么问题吗? I did simple execution in my MATLAB command window to simplify what I want to get as output. 我在MATLAB命令窗口中执行了简单的执行,以简化要输出的内容。

m=[4,12;6,8] m = [4,12; 6,8]

m = m =

 4    12
 6     8

max(m) 最大(米)

ans = 6 12 ans = 6 12

abs(m/max(m)) 绝对(米/最大(米))

ans = 回答=

0.9333
0.7333

I am stuck with this problem for a long time. 我长期困扰这个问题。 Please help. 请帮忙。

I interpret B3=abs(B2/max(B2)) as folllowing. 我将B3=abs(B2/max(B2))为如下。

  • b = max(B2) is a row vector containing the largest elements of the respective columns of B2 . b = max(B2)是包含B2各个列的最大元素的行向量。

  • q = B2/b means the least-squares solution to the overdetermined linear equations qb = B2 . q = B2/b表示超定线性方程qb = B2的最小二乘解。 (There are nrow independent problems, where nrow is the number of rows of B2 ). (有nrow独立的问题,其中nrowB2的行数)。 This equation is equivalent to b^T q^T = B2^T , where ^T is my notation for transpose, and this form is more frequently implemented in many libraries, I guess. 这个等式等效于b^T q^T = B2^T ,其中^T是我的转置符号,我猜想这种形式在许多库中更常见。

  • abs(q) means the elementwise absolute value of q . abs(q)指的绝对的elementwise值q

So, the required result is x below. 因此,所需的结果是下面的x Maybe. 也许。

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

Output is 输出是

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

cf. cf.

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


  • Below is my old answer based on misunderstanding of the definition of A/v in Matlab. 以下是我对Matlab中A/v定义的误解的旧答案。

Maybe the result B3 in the quection corresponds to the vector x below. 也许结果中的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 cf

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


Below is old and wrong answer based on my misunderstanding of max(A) in matlab. 以下是基于我对matlab中的max(A)误解而得出的古老而又错误的答案。

In Matlab, max(A) is the maximum element of the matrix A , and abs(A) returns a matrix taking the absolute values of the respective element of A . 在Matlab中, max(A)是矩阵A的最大元素,而abs(A)返回一个矩阵,该矩阵采用A的各个元素的绝对值。 So, if B2 is a Matrix object of eigen, maybe 因此,如果B2是本征矩阵对象,也许

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM