-5

I would like to select a part of a vector in C++ with the library Eigen?

I mean if I have this vector :

VectorXd v(6);
v << 1, 2, 3, 8, 1, 2;

Is there a function which returns this vector:

a << 2, 8, 2;

?

Because I know how to select just a part of a vector but only with a step of one not two.

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52

1 Answers1

2

There is no function that does this super specific thing. If it did it would probably be under eigen block operations. Your best bet is going to be to write your own function. Something similar to this:

void addConsecutiveSpacedElements(VectorXd &out, const VectorXd &in, int start, int space) {
    int index = 0;
    for (int in_index = start, in_index < in.size(); in_index += space) {
        out[index++] = in[in_index];
    }
}

...

addConsecutiveSpacedElements(a, v, 1, 2);

Note: This is essentially pseudo code and therefore has no error checking etc. It is also untested.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175