This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Concatenating vectors into matrix columns

Tags: None
(comma "," separated)
daviddoria
Registered Member
Posts
46
Karma
0
OS
I have a collection of vectors and I want to create a matrix using the vectors as the columns. I know about the comma initializer:

Code: Select all
void ConcatenateVectorIntoMatrixColumns()
{
  std::cout << "ConcatenateVectorIntoMatrixColumns()" << std::endl;

  Eigen::VectorXf v1(3);
  v1[0] = 1;
  v1[1] = 2;
  v1[2] = 3;

  Eigen::VectorXf v2(3);
  v2[0] = 4;
  v2[1] = 5;
  v2[2] = 6;

  Eigen::MatrixXf m(3,2);

  // This works
  m << v1, v2;

  // This does not work:
//   m << v1;
//   m << v2;

  std::cout << m << std::endl;

}


but I would want to do this in a loop, e.g.
Code: Select all
std::vector<Eigen::VectorXf> vectors;
// ... fill vectors ...
Eigen::MatrixXf M(vectors[0].size(), vectors.size());
for(int i = 0; i < vectors.size(); ++i)
  M << vectors[i];

but it won't let me because when trying to only write one vector into the matrix, there are not enough elements to fill the entire matrix (clearly). Is there a better way to construct this matrix from these vectors as columns without doing it element-wise?

Thanks,

David
manuels
Registered Member
Posts
47
Karma
0
Code: Select all
m.col(1) = v1;
m.col(2) = v2;
...
daviddoria
Registered Member
Posts
46
Karma
0
OS
Yep, that'll do it, thanks! So I guess the << and 'operator,' are only for times when you have all of the matrix to create expressed as a very small number of simple expressions.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell