LoginSignup
0
0

More than 1 year has passed since last update.

イテレータを用いたEigen::Matrix内の行・列単位でのアクセス

Posted at

Eigen 3.4 以降でMatrixの行/列をイテレータでアクセスできるみたいです. これでコンテナに格納されたVector3dをMatrixXdにまとめたり、その逆が簡単に書けるようになります.

sample.cpp
#include <Eigen/Dense>
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
        Eigen::MatrixXd M = Eigen::MatrixXd::Random(3, 12);
        std::cerr<<M<<std::endl<<std::endl;
        std::vector<Eigen::Vector3d> vecs;
        std::copy(M.colwise().begin(), M.colwise().end(), std::back_inserter(vecs));
        for(auto v : vecs) {
                std::cout<<v.transpose()<<std::endl;
        }
        return 0;
}

出力

 -0.999984 -0.0826997  -0.905911   0.869386   0.661931  0.0594004  -0.233169   0.373545   0.692334   0.307838   0.820642  -0.905071
 -0.736924  0.0655345   0.357729  -0.232996  -0.930856   0.342299  -0.866316   0.177953  0.0538576  -0.168001   0.524396   0.472164
  0.511211  -0.562082   0.358593  0.0388327  -0.893077  -0.984604  -0.165028   0.860873   -0.81607   0.402381  -0.475094  -0.343532

-0.999984 -0.736924  0.511211
-0.0826997  0.0655345  -0.562082
-0.905911  0.357729  0.358593
 0.869386 -0.232996 0.0388327
 0.661931 -0.930856 -0.893077
0.0594004  0.342299 -0.984604
-0.233169 -0.866316 -0.165028
0.373545 0.177953 0.860873
 0.692334 0.0538576  -0.81607
 0.307838 -0.168001  0.402381
 0.820642  0.524396 -0.475094
-0.905071  0.472164 -0.343532
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0