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])を複製するには、matrix クラスの.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 に答える