I am new to Eigen, and I would like to normalize a matrix in rowwise, so my code goes like this:
int buf[9];
for (int i{0}; i < 9; ++i) {
buf[i] = i;
}
m = Map<MatrixXi>(buf, 3,3);
MatrixXi mean = m.colwise().mean();
VectorXi m2 = Map<VectorXi>(mean.data(), mean.cols());
m.rowwise() -= m2;
This will not work, since m2
is interpreted as vertical, what is the cause of this ?
By the way, I just found that I could not avoid creating a mean
matrix, which I thinks I could:
// this works
MatrixXi mean = m.colwise().mean();
VectorXi m2 = Map<VectorXi>(mean.data(), mean.cols());
// this cannot pass the compilation check
VectorXi m2 = Map<VectorXi>(m.colwise().mean().data(), m.cols());
What maybe the cause of this then ?
Your question is not very clear but I guess you're looking for .transpose()
. Also no need to remap the result of .mean()
:
Map<MatrixXi> m(buf, 3,3);
VectorXi mean = m.colwise().mean();
m.rowwise() -= mean.transpose();
or directly use a row vector:
RowVectorXi mean = m.colwise().mean();
m.rowwise() -= mean;
or even the one-liner:
m.rowwise() -= m.colwise().mean().eval();