0

I am getting crazy with this seemingly simple question. I am translating code from Matlab to C++ with Eigen. In Matlab I have something like this:

v1 = zeros(100,1);
a=2;
b=5;
c=4;
v2 = [100 0.2 30 41 55];

v1(a:b:a+b*c) = v2;

In this way I set only the elements of v1 starting from a to a+b*c and equally spaced with b. The other elements are left unchanged. In Eigen, I currently solved with a for loop:

VectorXf v1 = VectorXf::Zero(100);
int a = 2;
int b = 5;
int c = 4;
VectorXf v2(5);
v2 << 100, 0.2, 30, 41, 55;

for (int i = 0; i < v2.size(); i++)
{
    v1(a-1+b*i) = v2(i);
}

I am looking for a way to do this without a loop. Is it possible? I have searched a lot without finding anything. Thank you very much.

user1941332
  • 55
  • 1
  • 6
  • This is how I would also solve it. – secdet Nov 18 '20 at 19:44
  • Check out the [Slicing and Indexing API](http://eigen.tuxfamily.org/dox-devel/group__TutorialSlicingIndexing.html) (this requires the master branch or the upcoming 3.4 version of Eigen) – chtz Nov 20 '20 at 00:34

1 Answers1

0

Solved thanks to @chtz.

int a = 2;
int b = 5;
int c = 4;
VectorXf v2(5);
v2 << 100, 0.2, 30, 41, 55;

VectorXf vNew = VectorXf::Zero(30);

vNew(Eigen::seq(a-1, a+b*c-1, b)) = v2;

The problem is, as suggested by the same @chtz, that "seq" cannot be used in the latest stable release of Eigen, the 3.3.8, because it lacks of the ArithmeticSequence.h file inside Eigen/src/core/ as reported here.

So, if you need to use it, you must download the unstable source code from the master branch here or the previous, deprecated, mirror on github here.

user1941332
  • 55
  • 1
  • 6