1

Is there anyway to apply the column-wise calculation as follows? (each column divided by the last entry of the column)

Eigen::MatrixXd A(3,5), B(3,5); 

A << 1, 4, 9, 16, 25,
     2, 4, 6, 8, 10,
     1, 2, 3, 4, 5;

B = (A.col) / (A.bottomerows<1>).col;

and B would be:

B = 1, 2, 3, 4, 5,
    2, 2, 2, 2, 2,
    1, 1, 1, 1, 1;
XJY95
  • 89
  • 6

1 Answers1

2

The functions you are looking for are .hnormalized() and .homogeneous(). Both can be applied .colwise() like this:

Eigen::MatrixXd B = A.colwise().hnormalized().colwise().homogeneous();

You can achieve the same with some .replicate() magic like this:

Eigen::MatrixXd B = A.array() / A.row(2).replicate(A.rows(),1).array();

(if A was an ArrayXXd, instead of a MatrixXd, you don't need to write the .array())

chtz
  • 17,329
  • 4
  • 26
  • 56