![](/img/trans.png)
[英]Using a pointer to dynamically allocate Matrices in c++ in presence of eigen library
[英]C++ Eigen: How to concatenate matrices dynamically (pointer issue?)
我有以下問題:我有幾個部分(本征)的MatrixXds,我想連接到另一個更大的,僅作為指針的MatrixXd變量。 但是,較小矩陣的大小及其數量都是動態的,因此我無法輕松使用<<運算符。
因此,我嘗試使用Eigen的MatrixXd塊函數,嘗試以下操作(較小的矩陣顯然存儲在list_subdiagrams中,而base-> cols()定義矩陣的數目):
// sd[] contains the smaller matrices to be concatenated; all are of the same size
// col defines the total number of smaller matrices
MatrixXd* ret = new MatrixXd(sd[0]->rows(), col*sd[0]->cols());
for (int i=0; i<col; ++i){
ret->block(0, i*sd[0]->cols(), sd[0]->rows(), sd[0]->cols()) = *(sd[i]);
}
不幸的是,這似乎以某種方式覆蓋了* ret變量的一部分-對於通過塊分配之前,大小(在我的測試用例中)正確顯示為2x1。 分配后變成140736006011136x140736006011376 ...
謝謝您的幫助!
您不知道尺寸是什么意思? 您可以使用成員函數cols()/rows()
來獲取大小。 另外,我假設通過串聯表示直接和? 在這種情況下,您可以執行以下操作
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::MatrixXd *A = new Eigen::MatrixXd(2, 2);
Eigen::MatrixXd *B = new Eigen::MatrixXd(3, 3);
*A << 1, 2, 3, 4;
*B << 5, 6, 7, 8, 9, 10, 11, 12, 13;
Eigen::MatrixXd *result = new Eigen::MatrixXd(A->rows() + B->rows(), A->cols() + B->cols());
result->Zero(A->rows() + B->rows(), A->cols() + B->cols());
result->block(0, 0, A->rows(), A->cols()) = *A;
result->block(A->rows(), A->cols(), B->rows(), B->cols()) = *B;
std::cout << *result << std::endl;
delete A;
delete B;
delete result;
}
因此,首先確保它適用於2個矩陣,對其進行測試,然后將其擴展為N
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.