Convert an Eigen matrix to Triplet form C++(将特征矩阵转换为三元组形式C++)
                            本文介绍了将特征矩阵转换为三元组形式C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我认为Eigen使用压缩方法存储稀疏矩阵。有没有什么方法可以从STD::Vectors中提取特征稀疏矩阵的三元组格式的向量?
谢谢。
详细信息(三元组格式示例) 矩阵的三元组格式:
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	Col	Val" <<endl;
    for (int k=0; k < A.outerSize(); ++k)
    {
        for (SparseMatrix<int>::InnerIterator it(A,k); it; ++it)
        {
            cout << 1+it.row() << "	"; // row index
            cout << 1+it.col() << "	"; // col index (here it is equal to k)
            cout << it.value() << endl;
        }
    }
    return 0;
}
                        这篇关于将特征矩阵转换为三元组形式C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:将特征矩阵转换为三元组形式C++
				
        
 
            
        
             猜你喜欢
        
	     - DoEvents 等效于 C++? 2021-01-01
 - 哪个更快:if (bool) 或 if(int)? 2022-01-01
 - OpenGL 对象的 RAII 包装器 2021-01-01
 - 将 hdc 内容复制到位图 2022-09-04
 - 如何提取 __VA_ARGS__? 2022-01-01
 - XML Schema 到 C++ 类 2022-01-01
 - 将函数的返回值分配给引用 C++? 2022-01-01
 - 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
 - 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
 - GDB 不显示函数名 2022-01-01
 
						
						
						
						
						