简体   繁体   中英

Load sparse matrix with Armadillo C++

I trying to load this matrix using Armadillo in C++ . This is my simple code:

#include <string>
#include <vector>
#include <iostream>
#include <armadillo>

int main(void) {

  arma::sp_mat matrix;
  matrix.load("./matrix/cfd1.mat", arma::raw_ascii);

  return 0;
}

At compile time the code doesn't generate any warning but when I run the executable file this is the output:

warning: SpMat::load(): unsupported file type

warning: SpMat::load(): couldn't read ./matrix/cfd1.mat

I tried to change the arma::raw_ascii with arma::hdf5_binary but it doesn't work the same.

EDIT

I think that is not a duplicate because the problem is not the header of the file. Even if I use arma::auto_detect the problem persist. If I use the type mat the problem there isn't.

Download the matrix in the so-called "matrix market" format: https://sparse.tamu.edu/MM/Rothberg/cfd1.tar.gz

Extract the matrix from the .tar.gz archive, obtaining cfd1/cfd1.mtx . The file has a header that has to be stripped away before the file can be loaded. Using a text editor, remove the first 14 lines in the mtx file. The first line should then have 1 1 1 . Save the edited file under the same name.

Load the edited matrix file in Armadillo using the coord_ascii option. You will need a recent version of Armadillo that supports coord_ascii (version 9.400 seems to work).

sp_mat X;
X.load("cfd1/cfd1.mtx", coord_ascii);

In Armadillo matrix indices start at 0 (due to C++ conventions), while in the matrix market file they start at 1. So you need to remove the first row and column from X to take this into account. This can be done via submatrices :

X = X.tail_rows(X.n_rows-1);
X = X.tail_cols(X.n_cols-1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM