# Eigen: concatenate matrix along a specific dimension

Eigen::MatrixXd V_src, V_target, V_merged;
V_merged.resize(V_src.rows() + V_target.rows(), 3);
V_merged << V_target, V_src;

# is Eigen make a deep copy of matrix?

```
void testSparseMatrix()
{
using namespace Eigen;
using namespace std;
typedef Triplet Trip;
std::vector trp, tmp;
// I subtracted 1 from the indices so that the output matches your question trp.push_back(Trip(1 - 1, 1 - 1, 3));
trp.push_back(Trip(1 - 1, 3 - 1, 4));
trp.push_back(Trip(2 - 1, 3 - 1, 1));
trp.push_back(Trip(3 - 1, 2 - 1, 2));
trp.push_back(Trip(3 - 1, 4 - 1, 5));
trp.push_back(Trip(4 - 1, 1 - 1, 4));
int rows, cols;
rows = cols = 4;
SparseMatrix A(rows, cols);
A.setFromTriplets(trp.begin(), trp.end());
cout &lt;&lt; "Matrix from triplets:" &lt;&lt; endl;
cout &lt;&lt; A &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; "Triplets:" &lt;&lt; endl;
cout &lt;&lt; "Row\tCol\tVal" &lt;&lt; endl;
for (int k = 0; k &lt; A.outerSize(); ++k)
{
for (SparseMatrix::InnerIterator it(A, k); it; ++it)
{
cout &lt;&lt; 1 + it.row() &lt;&lt; "\t"; // row index
cout &lt;&lt; 1 + it.col() &lt;&lt; "\t"; // col index (here it is equal to k)
cout &lt;&lt; it.value() &lt;&lt; endl;
}
}
//////////////////////////
SparseMatrix B; B = A;
std::cout &lt;&lt; "A\n" &lt;&lt; Eigen::MatrixXi(A) &lt;&lt; std::endl;
std::cout &lt;&lt; "B\n" &lt;&lt; Eigen::MatrixXi(B) &lt;&lt; std::endl;
B.coeffRef(0, 0) = 98;
std::cout &lt;&lt; "A\n" &lt;&lt; Eigen::MatrixXi(A) &lt;&lt; std::endl;
std::cout &lt;&lt; "B\n" &lt;&lt; Eigen::MatrixXi(B) &lt;&lt; std::endl;
std::cin.get();
}
```

output:

Matrix from triplets:
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0

Triplets:
Row Col Val
1 1 3
4 1 4
3 2 2
1 3 4
2 3 1
3 4 5
A
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
B
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
A
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
B
98 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0

# SparseMatrix: Setting an element to zero, suppresses all nonzeros which do not satisfy the predicate

Suppose you already have a sparse matrix A that you already used for some purpose like solving Ax=b, and now you would like to change a *few* entries in A, and in particular to remove some? If that’s so, then set them to 0 using A.coeffRef(i,j) = 0, and once you’re done, you can remove them with A.prune(0, 1e-8).

# print a sparse matrix with Eigen

To get nice formatting, you need to first convert it to a dense matrix:

``````Eigen::SparseMatrix<double> spmat;
...
std::cout << Eigen::MatrixXd(spmat) << std::endl;``````

https://stackoverflow.com/a/38562176/4862158

# Eigen column vector to row vector(auto converte)

No need to do transpose of column vector to pass it to a row vector.

Eigen::Vector3d v1;

Eigen::RowVector3d v2;

v1 << 1, 2, 3;

v2 << 4, 5, 6;

std::cout << “v1:\n” << v1 << std::endl;

std::cout << “v2:\n” << v2 << std::endl;

v2 = v1;

std::cout << “v2:\n” << v2 << std::endl;

results:

v1:

1

2

3

v2:

4 5 6

v2:

1

2

3

# How can an Eigen matrix be written to file in CSV format?

sing `format` is a bit more concise:

``````// define the format you want, you only need one instance of this...
const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", "\n");
``````

``````void writeToCSVfile(string name, MatrixXd matrix)
{
ofstream file(name.c_str());
file << matrix.format(CSVFormat);
}``````

https://stackoverflow.com/a/23566993/4862158