SomethingSomething
SomethingSomething

Reputation: 12178

Copying C++ array as a vector of Eigen's matrix

I have an Eigen matrix:

Eigen::Matrix<double, Eigen::Dynamic, VECTOR_SIZE> my_matrix(num_vectors, VECTOR_SIZE);

And I have a double array with VECTOR_SIZE elements:

double my_vector_data[VECTOR_SIZE];

I want to copy the data from the array my_vector_data into some row in my_matrix.

How can assign such a C++ vector into a specific row (vector) in the Eigen Matrix?

Upvotes: 1

Views: 808

Answers (2)

davidhigh
davidhigh

Reputation: 15468

If you want it more sophisticated than in the answer of @MichaelSmith, this should work for a std::array or raw array data of size VECTOR_SIZE:

using FixedSizeRowVector = Eigen::Matrix<double, 1, VECTOR_SIZE>;

size_t some_number = 10;
my_matrix.row(some_number) = Eigen::Map<FixedSizeRowVector> v(data);

I can't test it however at the moment.

Upvotes: 3

Michael Smith
Michael Smith

Reputation: 1331

You could copy the data to the matrix by doing the following:

for (int i = 0; i < VECTOR_SIZE; i++)
{
    my_matrix(0, i) = my_vector_data[i];
}

You can find more information about coefficient accessors here: https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html#title4.

Upvotes: 1

Related Questions