Marco Stamazza - 11 months ago 67

C++ Question

I need to reverse the order of the columns of a MatrixXd using RcppEigen.

In

`R`

`> M = matrix(1:9, ncol = 3)`

> M

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> M = M[, 3:1]

> M

[,1] [,2] [,3]

[1,] 7 4 1

[2,] 8 5 2

[3,] 9 6 3

In

`C++`

`Eigen::MatrixXd m1(3, 3);`

Eigen::MatrixXd m2(3, 3);

m1 << 1, 4, 7,

2, 5, 8,

3, 6, 9;

for (int i = 0; i < 3; i++){

m2.col(i) = m1.col(2 - i);

}

return m2;

Is there a better way of doing it? I looked also the

`Eigen::Array`

Thanks, Marco