简体   繁体   中英

How to create a large Eigen matrix from a 9222*9222 2d array in c++

I have tried mapping the matrix from the array using

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0][0]);

where A is double **A.

A = (double **)malloc((fullsize)*sizeof(double *));         //allocate memory dynamically for matrix A
    for (i = 0; i < fullsize; i++)
        A[i] = (double *)malloc((2 * fullsize)*sizeof(double));

but the program crashes.

I have tried

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0][0]);

but the program won't compile.

A is a pointer pointing to a contiguous field of pointers. These pointers point to a contiguous field of doubles. Thus A doesn't point to a contiguous field of doubles. &A[0] points to the first pointer, &A[0][0] points to the first element in the field of doubles pointed to by &A[0].

Don't use malloc in C++. Have look at this to create a contigous 2D field of doubles: C-style 2D-dynamic array with need to perform only one free() operation

Try this, Map reinterpret the array as a matrix.

double A[fullsize*fullsize];

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0]);

The vector version might work, but you give Map the address of the vector and not the data in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM