template<typename VectorType, int Size>
Eigen::VectorBlock class

Expression of a fixed-size or dynamic-size sub-vector.

Template parameters
VectorType the type of the object in which we are taking a sub-vector
Size size of the sub-vector we are taking at compile time (optional)

This class represents an expression of either a fixed-size or dynamic-size sub-vector. It is the return type of DenseBase::segment(Index,Index) and DenseBase::segment<int>(Index) and most of the time this is the only way it is used.

However, if you want to directly manipulate sub-vector expressions, for instance if you want to write a function returning such an expression, you will need to use this class.

Here is an example illustrating the dynamic case:

#include <Eigen/Core>
#include <iostream>
using namespace Eigen;
using namespace std;

template<typename Derived>
Eigen::VectorBlock<Derived>
segmentFromRange(MatrixBase<Derived>& v, int start, int end)
{
  return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
}

template<typename Derived>
const Eigen::VectorBlock<const Derived>
segmentFromRange(const MatrixBase<Derived>& v, int start, int end)
{
  return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start);
}

int main(int, char**)
{
  Matrix<int,1,6> v; v << 1,2,3,4,5,6;
  cout << segmentFromRange(2*v, 2, 4) << endl; // calls the const version
  segmentFromRange(v, 1, 3) *= 5;              // calls the non-const version
  cout << "Now the vector v is:" << endl << v << endl;
  return 0;
}

Output:

6 8
Now the vector v is:
 1 10 15  4  5  6

Here is an example illustrating the fixed-size case:

#include <Eigen/Core>
#include <iostream>
using namespace Eigen;
using namespace std;

template<typename Derived>
Eigen::VectorBlock<Derived, 2>
firstTwo(MatrixBase<Derived>& v)
{
  return Eigen::VectorBlock<Derived, 2>(v.derived(), 0);
}

template<typename Derived>
const Eigen::VectorBlock<const Derived, 2>
firstTwo(const MatrixBase<Derived>& v)
{
  return Eigen::VectorBlock<const Derived, 2>(v.derived(), 0);
}

int main(int, char**)
{
  Matrix<int,1,6> v; v << 1,2,3,4,5,6;
  cout << firstTwo(4*v) << endl; // calls the const version
  firstTwo(v) *= 2;              // calls the non-const version
  cout << "Now the vector v is:" << endl << v << endl;
  return 0;
}

Output:

4 8
Now the vector v is:
2 4 3 4 5 6

Base classes

template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
class Block
Expression of a fixed-size or dynamic-size block.

Constructors, destructors, conversion operators

VectorBlock(VectorType& vector, Index start, Index size)
VectorBlock(VectorType& vector, Index start)

Function documentation

template<typename VectorType, int Size>
Eigen::VectorBlock<VectorType, Size>::VectorBlock(VectorType& vector, Index start, Index size)

Dynamic-size constructor

template<typename VectorType, int Size>
Eigen::VectorBlock<VectorType, Size>::VectorBlock(VectorType& vector, Index start)

Fixed-size constructor