Submatrices and indices using Eigen

怎甘沉沦 提交于 2019-12-03 09:02:26

问题


I'm currently working on a MATLAB project and I'd like to re-implement the most computational-heavy parts using C++ and Eigen. I'd like to know if there's a way to perform the following operation (MATLAB syntax):

B = A(A < 3);

For those who are not familiar with MATLAB, the above-mentioned command initializes a matrix B made of the cells in A whose values are less than 3.

I've seen from a post on the Eigen forum that it's possible to obtain the indices of interest by using:

MatrixXi indices = (A.array() < 3).cast<int>();

What I'd like to have is something like:

MatrixXd B = A(A.array() < 3);

Thanks.


回答1:


There currently is a feature request for selecting sub-matrices by indexing filed at the Eigen BugTracker system. Therefore, I doubt it will be possible that way.

The only workaround I could think of is to copy the data manually. Not very nice though.




回答2:


libigl has many wrappers for Eigen to make it feel more like MATLAB. In particular, there is a slice function so that you can call:

igl::slice(A,indices,B);

which is equivalent to MATLAB's

B = A(indices)



回答3:


You can perform operations on selected elements only with select(), which is the equivalent for the ternary ?: operator. This is not exactly what you wanted, but should work in many cases.

MatrixXd B = (A.array() < 3).select(operation_on(A), MatrixXd::Zero(A.rows(), A.cols()));

This will fill B with zeros if A<3 and the result of any required operation on A otherwise.




回答4:


The latest development available on master branch of Eigen allows to work with numerical indices.

Here is a similar request, that shows an example of numerical indexing



来源:https://stackoverflow.com/questions/13540147/submatrices-and-indices-using-eigen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!