4

たとえば、行列を取得しました: C(400,400)

そして、この行列を1つのベクトルで成長させたいと思います.たとえば、行列の開始インデックス0にあるこの行列の行と列:

マトリックス C:

 3 2 5 
 4 5 6
 7 8 20

私の新しいベクトル: 25 5 6 8

結果 :

  25 5 6 8
  5  3 2 5 
  6  4 5 6
  8  7 8 20

Eigenでこれを行う最良の方法は何ですか? .resize()、および.set?を使用します。手伝ってくれてありがとう

4

2 に答える 2

3

私が思いつくことができる最高のものは次のとおりです。

Eigen::MatrixXd newC(C.rows()+1, C.cols()+1);
newC << v.transpose(), v.tail(v.size()-1), C;
C.swap(newC);

これは、「新しいベクトル」が に列ベクトルとして格納されていることを前提としていますv。このフラグメントの後、変数newCは不要になります。

于 2013-03-11T09:39:58.730 に答える
3

Though I would use Jitse's method as well, to give you another choice, here is a solution that is a bit more verbose and may show better where the parts go:

Eigen::MatrixXd newC(C.rows()+1, C.cols()+1);
newC.topRows<1>() = v.transpose();
newC.leftCols<1>() = v;
newC.bottomRightCorner(C.rows(),C.cols()) = C;
C.swap(newC)

Note that this assigns the top left element twice; if you don't want that, replace the third line with this ugly one:

newC.topRows<1>().tail(v.size()-1) = v.transpose().tail(v.size()-1);

As a final note as to why we don't use resize: it always throws away your matrix content (even when shrinking a matrix), except when the number of elements stays exactly the same, i.e. when you resize an MxN matrix to size (M*k)x(N/k).

于 2013-03-20T16:25:43.777 に答える