0

我正在尝试在 Eigen 中实现 Matlab 的 m(:, v > 0) 和 m(:,[1,3]) 。到目前为止,我得到了以下功能:

MatrixXd select_cols(const MatrixXd src, const Matrix<bool, 1, Dynamic> cond)                                                                                              
{                                                                                                                                                                          
    MatrixXd dst(src.rows(), cond.count());                                                                                                                                
    unsigned int i = 0;                                                                                                                                                    

    for (unsigned int j = 0; j < cond.cols(); j++) {                                                                                                                       
        if (cond(0,j)) {                                                                                                                                                   
            dst.col(i++) = src.col(j);                                                                                                                                     
        }                                                                                                                                                                  

    }                                                                                                                                                                      
    return dst;                                                                                                                                                            
}

MatrixXd select_cols(const MatrixXd src, const Matrix<unsigned int, 1, Dynamic> idx)                                                                                       
{                                                                                                                                                                          
    MatrixXd dst(src.rows(), idx.size());                                                                                                                                  

    for (unsigned int i = 0; i < idx.size(); ++i) {                                                                                                                        
        dst.col(i) = src.col(idx[i]);                                                                                                                                      
    }                                                                                                                                                                      

    return dst;                                                                                                                                                            
}       

但是,我认为以下代码段会起作用,但它不会:

Eigen::MatrixXd m1(3,3);                                                                                                                                                           
Eigen::Matrix<unsigned int, 1, Eigen::Dynamic> v(3);                                                                                                                                                                                                                                                                                                 
m1 << 1,2,3,                                                                                                                                                               
      4,5,6,                                                                                                                                                               
      7,8,9;                                                                                                                                                               

v  << 2,0,2;                                                                                                                                                               

Eigen::MatrixXd m2 = Eigen::select_cols(m1, v.array() > 1);

有错误:

utest.cpp:88:62: error: call of overloaded ‘select_cols(Eigen::MatrixXd&, const Eigen::CwiseUnaryOp<std::binder2nd<std::greater<unsigned int> >, const Eigen::ArrayWrapper<Eigen::Matrix<unsigned int, 1, -0x00000000000000001> > >)’ is ambiguous
utest.cpp:88:62: note: candidates are:
common.h:29:14: note: Eigen::MatrixXd Eigen::select_cols(Eigen::MatrixXd, Eigen::Matrix<unsigned int, 1, -0x00000000000000001>)
common.h:32:14: note: Eigen::MatrixXd Eigen::select_cols(Eigen::MatrixXd, Eigen::Matrix<bool, 1, -0x00000000000000001>)
4

1 回答 1

0

要在 Eigen 中复制 Matlab 的m(:,[1,3]),您可以简单地使用矩阵类中的.block()方法:

MatrixXd m(3,4);
m << 1,2,3,4,5,6,7,8,9,10,11,12;
...

MatrixXd sub_m = m.block(0,0, m.rows(),3);

...
std::cout << m << std::endl << sub_m << std::endl;

输出将是:

1  2  3  4
5  6  7  8
9 10 11 12
1  2  3
5  6  7
9 10 11

问候。

于 2014-04-14T09:23:54.837 回答