Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created February 17, 2015 06:58
Show Gist options
  • Save jefftrull/25fba71dae683bc48f74 to your computer and use it in GitHub Desktop.
Save jefftrull/25fba71dae683bc48f74 to your computer and use it in GitHub Desktop.
Demo of getting a dense vector from a row of a sparse matrix in Eigen
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Sparse>
int main() {
using namespace std;
using namespace Eigen;
vector<Triplet<double> > tripletList;
tripletList.push_back(Triplet<double>(0, 0, 1.0));
tripletList.push_back(Triplet<double>(0, 1, 0.0));
tripletList.push_back(Triplet<double>(1, 0, 0.0));
tripletList.push_back(Triplet<double>(1, 1, 1.0));
SparseMatrix<double, RowMajor> m(2,2);
m.setFromTriplets(tripletList.begin(), tripletList.end());
VectorXd v = m.row(0);
cout << "v=\n" << v << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment