Matrix with an unknown number of rows and columns Own library

I want to read data from a file to a matrix in Eigen. I have encoded everything, but there is one problem that I am facing. I don’t know in advance how much data is in the file, so I want to be able to initialize the matrix without specifying its size. I know that the following matrix initialization method works in Eigen:

MatrixXd A;

But now, if I then do, for example,

A << 1, 2,
     4, 7;

This does not work. I was hoping he would recognize this as a 2x2 matrix in this example so that I could work with it. So basically my question is: how can I add data to A without specifying its size?

+4
source share
3 answers

, , std::vector std::vector Map:

MatrixXf A;
std::vector<float> entries;
int rows(0), cols(0);
while(...) { entries.push_back(...); /* update rows/cols*/ }
A = MatrixXf::Map(&entries[0], rows, cols);

, conservativeResize .

+4

Eigen Tutioral Matrix


, Eigen , . RowsAtCompileTime ColsAtCompileTime Dynamic, , , . Eigen ; , , . , typedef MatrixXd, :

typedef Matrix<double, Dynamic, Dynamic> MatrixXd;

typedef VectorXi :

typedef Matrix<int, Dynamic, 1> VectorXi;

, , , :

Matrix<float, 3, Dynamic>

, :

Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> myMatrix;
myMatrix.resize(1, 1);
myMatrix(0, 0) = 1.0;
myMatrix.resize(2, 2);
myMatrix(1, 1) = 1.0;
myMatrix.resize(3, 3);
myMatrix(2, 2) = 1.0;
myMatrix.resize(4, 4);
myMatrix(3, 3) = 1.0;
+2

, . , ? , 4x1 1x4. ++, , m << 1,2,3,4;.

, , . , / , , , .

Matrix m; 0x0. .

Eg.

#include<Eigen/Core>
#include<iostream>

int main(){
Eigen::MatrixXd m;
m.resize(2,2);
m << 1, 2, 3, 4;
std::cout << m << '\n';
m.resize(1,4);
std::cout << m << '\n';
}
0

Source: https://habr.com/ru/post/1538895/


All Articles