How to copy Eigen matrix
Asked Answered
H

1

6

I have two Eigen::MatrixXd and they always have a single row. The input matrix is A and I want to copy this matrix into another matrix B, but the number of columns between the matrices can be different.

Following is an example:

A
 0.5

And I need to create a B matrix of 1 rows and 4 columns, so that it will be:

B
 0.5 0.5 0.5 0.5

But if A is:

A
 1 0.5

Then B will be

B
 1 0.5 1 0.5

How can I do?

Harbour answered 19/11, 2015 at 15:54 Comment(0)
M
9

You can replicate a matrix by using the (wait for it) replicate function. The first parameter is how many times to repeat the rows, the second is the number of times to repeat the columns.

#include <iostream>
#include <Eigen/Core>

int main()
{
    Eigen::MatrixXd a(1, 2), b;
    a << 1, 0.5;
    b = a.replicate(1, 2);
    std::cout << a << "\nbecomes:\n" << b << std::endl;

    return 0;
}

gives

1 0.5
becomes:
1 0.5 1 0.5

Moffitt answered 19/11, 2015 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.