Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy Eigen matrix

Tags:

c++

matrix

eigen

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?

like image 295
Nick Avatar asked Nov 19 '15 15:11

Nick


1 Answers

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

like image 154
Avi Ginsburg Avatar answered Oct 12 '22 13:10

Avi Ginsburg