Replicate a column VectorXd in order to construct a MatrixXd in Eigen, C++ -


let's assume have 10x20 real matrix:

eigen::matrixxd a(10,20); a.setrandom(); 

we construct 10x10 matrix of form

b = [v v ... v v]

where v column vector of length 10. vector, v, each element squared norm of each row of a, is:

v = ( ||x_1||^2, ||x_2||^2, ..., ||x_10||^2,)^t,

where x_j denotes j-th row of a.

what efficient way construct matrix b?

i construct v follows:

eigen::vectorxd v(10); (int i=1; i<10; i++) {     v(i) = a.row(i).squarednorm(); } 

i think step cannot solved without for loop. how replicate column 10 times such b filled discussed above?

your assumption wrong. loop can avoided doing rowwise operation. then, replication can done follows.

#include <iostream> #include <eigen/core>  int main () {     eigen::matrixxd a(10,20), b, c;     a.setrandom();      eigen::vectorxd v(10);     v = a.rowwise().squarednorm();      b = v.replicate(1,10);      std::cout << b << "\n\n";      return 0; } 

it can written in single line as

    b =  a.rowwise().squarednorm().replicate(1,10); 

i highly recommend reading documentation. it's pretty written.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -