user1612986
user1612986

Reputation: 1415

Eigen Matrix Library index operation

Say I have

  Eigen::VectorXd r = Eigen::VectorXd::Random(10); 

now I want the following:

  double lb1(-0.1);
  double ub1(0.1);
  double v(5.); 
  for(int i =0;i<10;i++)
      if( (lb1 < r[i]) && (r[i]<ub1))
            r[i] = v;

there are many non overlapping (lb1,ub1) and many "v". Is there a easy elegant way to perform this computation without writing two loops (I have a matlab kind of operation in mind)

Thanks in advance for any help.

Upvotes: 0

Views: 461

Answers (1)

kangshiyin
kangshiyin

Reputation: 9781

You could use .select()

r = (r.array() > lb1 && r.array() < ub1).select(v, r);

Upvotes: 2

Related Questions