Does the Eigen Matrix support vector indexing?

For example, if I have a 4 by 4 matrix. Is there a way to create another matrix (or representation to the original matrix, even better), that is, only rows 1 and 3 of the original matrix.

I only see how to extract a row or block, but not what I mentioned above. Here is my code:

#include <iostream>
#include <Eigen/Dense>

using namespace Eigen;

int main()
{
  Matrix4f m = Matrix4f::Random();
  std::cout << "Matrix : " << std::endl;
  std::cout << m << std::endl;

  std::cout << "row" << std::endl;
  std::cout << m.row(1) << std::endl;

  std::cout << "block : " << std::endl;
  std::cout << m.block(1,0,2,4) << std::endl;
  return 0;
}

      

A potential solution is to pre-multiply my matrix by a matrix of ones and zeros,

 z = ([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.]])

      

z * m would give me what I want, but there is a better solution.

Edit:

Possible application of what I want to do:

Let's say I have matrices A(m x n)

and B(n x k)

. I want to select from A

and multiplied by B

, say, I take 1/5th

from the rows A A'(m/5 X n) * B(n x k)

. I do not need A'

myself, this is the product that I am after him.

+3


source to share


1 answer


A native permutation matrix might be what you're looking for:

Arbitrarily rearrange the rows / columns of a matrix with its own



using Eigen;
Matrix4f m = Matrix4f::Random();

PermutationMatrix<Dynamic,Dynamic> P(4);
perm.indices()[0] = 1;
perm.indices()[1] = 3;

MatrixXf B = (m * P).leftCols(2);

      

0


source







All Articles