Ok, say for example you have a 3x3 matrix:
m = [3 -1 1; 2.5 1.5 6; 4 7 1]
and say you want to extract following rows from m matrix:
[0 2], // row 0 and row 2
essentially giving out following matrix:
new_extracted_matrix = [3 -1 1; 4 7 1] // row 0 and row 2 of matrix m
Main thing here is, let's create a vector v having contents [0 2], means we would extract following row indices from matrix m.
Here is what i did:
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main()
{
Matrix3f m;
m(0,0) = 3;
m(0,1) = -1;
m(0,2) = 1;
m(1,0) = 2.5;
m(1,1) = m(1,0) + m(0,1);
m(1,2) = 6;
m(2,0) = 4;
m(2,1) = 7;
m(2,2) = 1;
std::cout << "Here is the matrix m:\n" << m << std::endl; // Creating a random 3x3 matrix
VectorXf v(2);
v(0) = 0; // Extracting row 0
v(1) = 2; // Extracting row 2
MatrixXf r(1,v.size());
for (int i=0;i<v.size();i++)
{
r.col(i) << v(i); // Creating indice vector
}
cout << "The extracted row indicies of above matrix: " << endl << r << endl;
MatrixXf N = MatrixXf::Zero(r.size(),m.cols());
for (int z=0;z<r.size();z++)
{
N.row(z) = m.row(r(z));
}
cout << "Extracted rows of given matrix: " << endl << N << endl;
}
This would give us following output:
Here is the matrix m:
[3 -1 1; 2.5 1.5 6; 4 7 1]
The extracted row indicies of above matrix:
[0 2]
Extracted rows of given matrix:
[3 -1 1; 4 7 1]
block
operations are useful for getting subsets of Eigen matrices. – Traditional