如何在C ++中使用特征库进行矩阵分割 [英] How to perform matrix matrix division using eigen library in C++

查看:294
本文介绍了如何在C ++中使用特征库进行矩阵分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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



其中`B2`是一个`nxm`矩阵。什么应该是使用Eigen库的等效`C ++`代码?请帮忙。

修改我的代码



//问题

#include< iostream>

#include< complex.h>

#include< eigen3 eigen =dense =>

#include< eigen3 eigen =core =>



使用名称空间Eigen;

使用名称空间std;

使用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<< 解决方案是A:\ n<< B.cwiseAbs()<<结束;



返回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< BinaryOp,Lhs,Rhs> :: CwiseBinaryOp(const Lhs&,const Rhs&,const BinaryOp&)[with BinaryOp = Eigen :: internal :: scalar_quotient_op< double,double> ;; LhsType = const Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; RhsType = const Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; Eigen :: CwiseBinaryOp< BinaryOp,Lhs,Rhs> :: Lhs = Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; Eigen :: CwiseBinaryOp< BinaryOp,Lhs,Rhs> :: Rhs = Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1> >]:断言`aLhs.rows()== aRhs.rows()&& aLhs.cols()== aRhs.cols()'失败。

流产(核心倾销)



任何想法有什么问题? ?请帮忙。

I did a `MATLAB` code and it had to perform


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

where `B2` is an `n x m` matrix . What should be the equivalent `C++` code using Eigen library? Please help.
On modifying my code

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

What I have tried:

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$ ./mm_t
mm_t: /usr/local/include/eigen3/Eigen/src/Core/CwiseBinaryOp.h:110: Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::CwiseBinaryOp(const Lhs&, const Rhs&, const BinaryOp&) [with BinaryOp = Eigen::internal::scalar_quotient_op<double, double>; LhsType = const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; RhsType = const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::Lhs = Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::Rhs = Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >]: Assertion `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed.
Aborted (core dumped)

Any idea what is wrong?? Please help.

推荐答案

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< BinaryOp,Lhs,Rhs> :: CwiseBinaryOp(const Lhs&,const Rhs&,const BinaryOp&)[with BinaryOp = Eigen :: internal :: scalar_quotient_op< double,double> ;; LhsType = const Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; RhsType = const Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; Eigen :: CwiseBinaryOp< BinaryOp,Lhs,Rhs> :: Lhs = Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1>取代; Eigen :: CwiseBinaryOp< BinaryOp,Lhs,Rhs> :: Rhs = Eigen :: ArrayWrapper< Eigen :: Matrix< double,-1,-1> >]:断言`aLhs.rows()== aRhs.rows()&& aLhs.cols()== aRhs.cols()'失败。

流产(核心倾销)



任何想法有什么问题? ?请帮忙。
./mm_t
mm_t: /usr/local/include/eigen3/Eigen/src/Core/CwiseBinaryOp.h:110: Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::CwiseBinaryOp(const Lhs&, const Rhs&, const BinaryOp&) [with BinaryOp = Eigen::internal::scalar_quotient_op<double, double>; LhsType = const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; RhsType = const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::Lhs = Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >; Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::Rhs = Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >]: Assertion `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed.
Aborted (core dumped)

Any idea what is wrong?? Please help.


你需要了解矩阵背后的数学:

如何划分矩阵(带图片) - wikiHow [ ^ ]



你不能直接在另一个矩阵上划分一个矩阵。
You need to understand the math behind the matrix:
How to Divide Matrices (with Pictures) - wikiHow[^]

You cant divide one matrix on another matrix directly.


这篇关于如何在C ++中使用特征库进行矩阵分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆