This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Collection of Eigen::Vectors - via std-Containers or matrix?

Tags: None
(comma "," separated)
benjones
Registered Member
Posts
1
Karma
0
Hi,

there are two possibilities to store vectors that I'd like your opinion/advice on:

(1) store them in std containers, e.g.

Code: Select all
std::vector< Eigen::Vector3d > vec;
for ( int i = 0; i < 5; i++)
    vec.push_back( Eigen::Vector3() );


(2) store them in a matrix, e.g.

Code: Select all
Eigen::Matrix< double, Dynamic, 3 > mat;
for ( int i = 0; i < 5; i++)
    mat.row(i) = Eigen::Vector3();



Are there reasons to favor one over the other by means of speed, possible operations, ...?

Best,
Ben
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Using a matrix as the advantage to allow you to work on the full set of 3D vectors at once, for instance to apply a rotation matrix:

Matrix3d rot;
rotated_pts = rot * pts;

or to compute the centroid:

Vector3d c = pts.rowwise().mean();

etc.

In this case, it is better to store them column-wise, so using a Matrix<double,3,Dynamic>.


The std::vector approach implies loops but is more efficient at dynamically resizing the number of vectors. Note that you can also see a vector<Vector3d> as a Matrix<double,3,Dynamic> using Map:

Code: Select all
std::vetcor<Vector3d> vec_pts(n);
...
Map<Matrix<double,3,Dynamic> >  pts(vec_pts.data().data(), 3, n);


In this case pts aliases vec_pts.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]