Reputation: 127
I am trying to add a vector to each row in a matrix, but I am getting error.
Here is the code:
MatrixXd shifted_cord = MatrixXd::Random(10,3);
Vector3d v = Vector3d(3.0,20.0,0.0);
//std::cout<<shifted_cord<<std::endl;
shifted_cord = shifted_cord.colwise()+v; //(10,3)
I need to shift all rows, in this case 10.
Same thing is done in this, but when I tried, I am getting error.
Error:
Assertion `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed.
Can anyone help me with this error?
Upvotes: 3
Views: 3114
Reputation: 18827
If you want to add to each row, you need to write .rowwise()
and make sure that the vector you add is indeed a row-vector (either declare v
as RowVector3d
, or write v.transpose()
. Also you can use the +=
operator with .rowwise()
expressions:
shifted_cord = shifted_cord.rowwise()+v.transpose();
// or
shifted_cord.rowwise()+=v.transpose();
Upvotes: 4