How to copy Eigen matrix

≡放荡痞女 提交于 2019-12-13 15:01:25

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/33808561/how-to-copy-eigen-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!