Jang
Jang

Reputation: 1

Overwriting and extending array or matrix without its size definition in Eigen

I want to extend and overlay arrays like below.

Example(array A&B) :

 A=   1   2
      4   8

 B = -1  -2
     -3  -4

Result(overwrting B on A from A.row(1) ) :

 C= 1  2 
   -1 -2
   -3 -4

Below code defines the size of Matrix before it creates and cannot overwrite on another matrix.

 #include <iostream>
 #include <stdlib.h>
 #include <Eigen/Dense>
 int main (int argc, char * argv [])
 {
    MatrixXd m (2, 2);
    m << 1, 2, 4, 8;
    MatrixXd n (2, 2);
    n << -1, -2, -3, -4;
    MatrixXd k (4, 2);
    k << 
       m,
       n;

     std::cout << k << std::endl;

    return EXIT_SUCCESS;
}

Is there a way to get the result without size definition (i.e. without this: k (4, 2);)?

Upvotes: 0

Views: 354

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

You can do what you asked in the first part of your question using block operations. For example, you could modify your example to be

 #include <iostream>
 #include <Eigen/Core>
 int main ()
 {
    Eigen::MatrixXd m (2, 2);
    m << 1, 2, 4, 8;
    Eigen::MatrixXd n (2, 2);
    n << -1, -2, -3, -4;
    Eigen::MatrixXd c (3, 2);
    std::cout << c << "\n\n";
    c.topRows(2) = m;
    std::cout << c << "\n\n";
    c.bottomRows(2) = n;

    std::cout << c << "\n\n";

    return 0;
}

You can either use the more general block() or use specific blocks as I did.

Regarding whether you can skip the definition of the matrix dimensions (k (4, 2);) the answer is not really, unless you assign a matrix of the correct dimensions in which case the sizing is implicit.

Upvotes: 1

Related Questions