简体   繁体   中英

Submatrix with stride in Eigen Library

I'm new to Eigen and I would like to create 10 mxn matrices. For some reasons I do it with the following method:

Matrix<double, m, n*10>

Which It seems that the memory allocation will be similar to the following:

 _______________________________________________________________
|M1(1,1)|M2(1,1)|...|M10(1,1)|.....|M1(1,n)|M2(1,n)|...|M10(1,n)|
|  . .                                                          |
|  .                                                            |

Now how it possible to create a reference matrix (means by reference and without copying data) to each of this 10 matrices?

I would recommend using the dynamically allocated matrix, as m and n might be large. Also, it appears that you assume the matrix memory is row major, when the default is column major. In the example below, I've explicitly made them row major.

You can use a series Eigen::Map<MatrixXd> s like so:

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

using namespace Eigen;

int main(void)
{
    int m = 3;
    int n = 4;
    int x = 6;
    typedef Matrix < double, Dynamic, Dynamic, RowMajor > ourMat;
    ourMat  M1(m, n * x);
    M1.setConstant(9.9);

    for (int i = 0; i < x; i++)
    {
        Eigen::Map<ourMat, 0, Stride<Dynamic, Dynamic>> m_i(M1.data() + i,
                                                            m, n,
                                   Stride<Dynamic, Dynamic>(n*x,x));
        m_i.setConstant(double(i));
        std::cout << m_i << std::endl;
        std::cout << M1 << "\n" << std::endl;

    }

    Eigen::Map<VectorXd> m_i(M1.data(), m * n * x);
    std::cout << m_i.transpose() << std::endl;

    return 0;
}

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