问题描述
我的问题是如何初始化特征矩阵,但不:
= vector.back()这不工作。解决方案它不快或有效,但它的工作原理:
#includetopo.h
#include< iostream>
#include< fstream>
#include< vector>
#include< Eigen / Dense>
#include< Eigen / Sparse>
#include #include< algorithm>
使用namespace std;
using namespace Eigen;
/ **从File读取数据并将其作为字符串存储在向量中** /
int readFromFile(const char * path,vector< string>& mv)// muss vector vorher调整大小werden? wan ja lese zeilenanzahl
{
fstream file;
string line;
file.open(path);
while(getline(file,line))// lese zeilefürzeile
{
mv.push_back(line); //füllevector von hinten last in first
}
file.close();
return 0;
}
typedef Matrix< int,4,4> MyMatrix; // Matrixspäterdynamisch
/ **解析要用作特征矩阵的数据** /
int fromVectoEigen(vector< string& source,MyMatrix& target)
{/ **将字符串转换为int并将其写入二维数组** /
int array [4] [4]; // noch resize nach vectorsize - > matrizen sind quadratisch
int i = source.size();
for(i = i-1; i> = 0; i--)// da nur von hintern auf vector zugreifbar auch von hinten bei array anfangen
{
string myString = source 。背部(); // leztzes Element von Vector als String
stringstream ssin(myString);
int j = 0;
while(ssin.good()&& j< 4)// auch hier vectorsizespäterdynamisch
{
ssin> array [j] [i]; //füllespalten in i.ter zeile
++ j;
}
source.pop_back(); // array.Leztes元素
}
// cout ';
// cout<< array [0] [1]<< array [1] [1] <<'\\\
';
// cout<< array [0] [2]<< array [1] [2] <<'\\\
';
// cout<< array [0] [3]<< array [1] [3] <<'\\\
';
//
/ **从二维数组到一维数组** /
int newarray [16]; // vectorsize * vectorsize
int k = 0;
for(int i = 0; i {for(int j = 0; j< 4; j ++)// vectorsize
{
newarray [k] = array [j] [i];
k ++;
}
}
/ **从数组创建Eigen矩阵** /
target = Map< Matrix4i>(newarray);
target.transposeInPlace();
cout<< target<<'\\\
';
return 0;
}
My question is how to initialize an eigen Matrix, but NOT this way:
matrix << 1,0,1,0, 1,0,1,0, 1,0,1,0,I have a Matrix that looks like the above one ( commas or no commas doesnt matter)stored in a txt file.
I already wrote a function to read in each line and put it into a vectornow I want to create a matrix with this data
But it doesn' work and I cant find any page that explains how to assign data to a matrix without writing just the values.(like the example above)
All I need is the data from my file in an eigen Matrix
What I tried so far: (PS: had the idea with the iterators but i guess it will take too long with really big matrices, I just tried this example with a 1-2 dimensional matrix)
int readFromFile (const char * path, vector <string> & mv) { fstream file; string line; file.open(path); while (getline(file,line)) { mv.push_back(line); } file.close(); return 0; } typedef Matrix <int, 1, 2> MyMatrix; int fromVectoEigen (vector<string> & source, MyMatrix & target) { //for (int i = source.size(); i<0 ; i--) //{ string valuerow = source.back(); string::iterator it = valuerow.begin(); target.row(0)<< *it; target.row(0)<<*it+1; //source.pop_back(); //} return 0; }Unfortunately cant just say Matrix.row(i) = vector.back() that doesnt work.
解决方案I think I found a solution! Its not fast or efficient but it works:
#include "topo.h" #include <iostream> #include <fstream> #include <vector> #include <Eigen/Dense> #include <Eigen/Sparse> #include <iterator> #include <algorithm> using namespace std; using namespace Eigen; /**Read data from File and store it in vector as string**/ int readFromFile (const char * path, vector <string> & mv) // muss vector vorher resized werden? wenn ja lese zeilenanzahl { fstream file; string line; file.open(path); while (getline(file,line)) // lese zeile für zeile { mv.push_back(line); //fülle vector von hinten last in first } file.close(); return 0; } typedef Matrix <int, 4, 4> MyMatrix; // Matrix später dynamisch /**Parsing data to be used as Eigen Matrix**/ int fromVectoEigen (vector<string> & source, MyMatrix & target) { /**convert string to int and write it to the two dimensional array **/ int array [4][4]; // noch resize nach vectorsize -->matrizen sind quadratisch int i = source.size(); for ( i= i-1 ; i >= 0 ; i-- ) // da nur von hintern auf vector zugreifbar auch von hinten bei array anfangen { string myString = source.back(); // leztzes Element von Vector als String stringstream ssin(myString); int j = 0; while (ssin.good() && j < 4) // auch hier vectorsize später dynamisch { ssin >> array[j][i]; // fülle spalten in i.ter zeile ++j; } source.pop_back(); //lösche letztes element } // cout<<array[0][0]<<array[1][0]<<array[2][0]<<array[3][0]<<'\n'; // cout<<array[0][1]<<array[1][1]<<array[2][1]<<array[3][1]<<'\n'; // cout<<array[0][2]<<array[1][2]<<array[2][2]<<array[3][2]<<'\n'; // cout<<array[0][3]<<array[1][3]<<array[2][3]<<array[3][3]<<'\n'; // /** from 2 dimensional array to one dimensional array**/ int newarray [16]; // vectorsize * vectorsize int k = 0; for ( int i = 0 ; i< 4 ; i++) // vectorsize { for (int j = 0 ; j<4; j++) // vectorsize { newarray[k]=array[j][i]; k++; } } /**create Eigen Matrix from Array**/ target= Map<Matrix4i>(newarray); target.transposeInPlace(); cout<<target<<'\n'; return 0 ; }
这篇关于特征库 - >用来自文件或现有std :: vector< string>的数据初始化矩阵。内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!