4
我認爲Eigen使用壓縮方法來存儲稀疏矩陣。有什麼方法可以從std :: vectors中提取特徵稀疏矩陣的Triplet格式向量?將特徵矩陣轉換爲三元組形式C++
謝謝。
A=
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
i = 1 1 2 3 3 4 // row
j = 1 3 3 2 4 1 // column
S = 3 4 1 2 5 4 // values
我認爲Eigen使用壓縮方法來存儲稀疏矩陣。有什麼方法可以從std :: vectors中提取特徵稀疏矩陣的Triplet格式向量?將特徵矩陣轉換爲三元組形式C++
謝謝。
A=
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
i = 1 1 2 3 3 4 // row
j = 1 3 3 2 4 1 // column
S = 3 4 1 2 5 4 // values
只需如圖所示tutorial:
更多信息(三重格式的例子)矩陣的 三重格式
#include <Eigen/Sparse>
#include <iostream>
using namespace Eigen;
using std::cout;
using std::endl;
typedef Triplet<int> Trip;
int main(int argc, char *argv[]){
std::vector<Trip> 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<int> A(rows,cols);
A.setFromTriplets(trp.begin(), trp.end());
cout << "Matrix from triplets:" << endl;
cout << A << endl;
cout << endl << "Triplets:" << endl;
cout << "Row\tCol\tVal" <<endl;
for (int k=0; k < A.outerSize(); ++k)
{
for (SparseMatrix<int>::InnerIterator it(A,k); it; ++it)
{
cout << 1+it.row() << "\t"; // row index
cout << 1+it.col() << "\t"; // col index (here it is equal to k)
cout << it.value() << endl;
}
}
return 0;
}
謝謝您的答覆。但是,這是如何從三重向量製作矩陣。我正在尋找「如何獲得已經存在於Eigen格式中的矩陣的三重向量」。 – 2015-02-25 15:13:55
@SinaJ請參閱第二部分。而不是打印它,構建三胞胎。 – 2015-02-25 18:22:46
@SinaJ如果你想訪問原始指針,可以通過'm.valuePtr()','m.outerIndexPtr()'和'm.innerIndexPtr()'來訪問它們,但它們的長度不一樣。不能天真地轉換爲三胞胎)。 – 2015-02-26 11:59:02