Search code examples
eigeneigen3

Eigen: map non-continous data in an arra with stride


I have a data array (double *) in memory which looks like:

[x0,y0,z0,junk,x1,y1,z1,junk,...]

I would like to map it to an Eigen vector and virtually remove the junk values by doing something like:

Eigen::Map<
  Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::ColMajor>,
  Eigen::Unaligned,
  Eigen::OuterStride<4>
  >

But it does not work because the outerstride seems to be restricted to 2D matrices.

Is there a trick to do what I want?

Many thanks!


Solution

  • With the head of Eigen, you can map it as a 2D matrix and then view it as a 1D vector:

    auto m1 = Matrix<double,3,Dynamic>::Map(ptr, 3, n, OuterStride<4>());
    auto v = m1.reshaped(); // new in future Eigen 3.4
    

    But be aware accesses to such a v involve costly integer division/modulo.