犰狳 C++:无法将浮点二维数组写入 fmat

armadillo C++: Cannot write float 2D array to fmat

我有一个 2 float 二维数组和一维数组。我想创建 fmat 变量并将二维数组写入其中,然后将一维数组写入向量,然后使用高斯消元法求解。当我尝试将二维数组写入变量时,我得到了结果:[matrix 1:0] in the other Segmentation fault

fmat A;
for(int i=0; i<elements+1; ++i)
{
    for(int j=0; j<elements+1; ++j)
        A << globalMatrix[i][j];
    A << endr;
}

cout<<"MATRIX\n\n";
A.print();

fvec B(elements+1);

for(int i=0;i<elements+1;++i)
    B=loadVec[i];

cout<<B;

fmatclass不是流,所以不能在循环中使用<<操作。相反,只需复制元素即可。您还需要记住 Armadillo 将矩阵存储在 column-major order (for compatibility with LAPACK). See the Armadillo documentation for more information about accessing elements.

fmat A(elements+1, elements+1, fill::zeros);

for(unsigned int i=0; i<elements+1; ++i)
for(unsigned int j=0; j<elements+1; ++j)
  {
  A(i,j) = globalMatrix[i][j];
  }

A.print("A:");


fvec B(elements+1);

for(unsigned int i=0; i<elements+1; ++i)
  {
  B(i) = loadVec[i];
  }

B.print("B:");