Expiredmind
Expiredmind

Reputation: 808

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

I have a 2 float 2D array and 1D array. I want to create fmat variable and write 2D array to it and then 1D to vector and then solve it using Gaussion elimination. When I try write 2d Array to variable I got result: [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;

Upvotes: 0

Views: 208

Answers (1)

hbrerkere
hbrerkere

Reputation: 1605

The fmat class is not a stream, so you can't use the << operation in a loop. Instead, simply copy the elements across. You will also need to bear in mind that Armadillo stores matrices in 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:");

Upvotes: 1

Related Questions