This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Mapping non-contiguous data

Tags: None
(comma "," separated)
Manhigh
Registered Member
Posts
10
Karma
0
OS

Mapping non-contiguous data

Fri Oct 26, 2012 5:23 pm
I have a row-major matrix stored in memory (say A = 10x20 )

I'm interested in accessing columns of that matrix, and they're easier to work with if I treat each column as a matrix. That is, I want to map A.col(3) to a 2x5 matrix. If A was column-major this would be trivial, but since it's not, I have to tell Map what stride to use, and I'm having trouble getting code to compile. Can anyone please give me a pointer as to how to set this up?

In the following code column3_as_2x5 should come out as [ 3 3 3 3 3; 3 3 3 3 3]

Code: Select all
#include <Eigen/Dense>
#include <iostream>

int main() {

    typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> MatrixXdrm;
   
    MatrixXdrm A(10,10);
   
    //Eigen::Map<MatrixXdrm> column3(A.col(3).data(),10,1);
    //Eigen::Map<MatrixXdrm> column3_as_2x5(A.col(3).data(),2,5);
   
    MatrixXdrm::ColXpr column3 = A.col(3);
   
    // COMPILATION FAILS - NO MATCHING FUNCTION CALL
    //Eigen::Map<MatrixXdrm> column3_as_2x5(column3.data(),2,5,Eigen::Stride<Eigen::Dynamic,Eigen::Dynamic>(1, 10));
   
    A.setZero();
   
    for(unsigned int i=0; i<10; i++) {
        A.col(i).fill( static_cast<double>(i) );
    } 

    std::cout << A << std::endl;
   
    std::cout << column3 << std::endl;
   
    //std::cout << column3_as_2x5 << std::endl;

    return 0;
}
Manhigh
Registered Member
Posts
10
Karma
0
OS

Re: Mapping non-contiguous data  Topic is solved

Mon Oct 29, 2012 6:31 pm
I had to draw the matrix to get the strides right, but heres the solution for mapping it to both column-major and row-major matrices:
Code: Select all
#include <Eigen/Dense>
#include <iostream>

int main() {

    typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> MatrixXdrm;
   
    MatrixXdrm A(10,10);
   
    MatrixXdrm::ColXpr column3 = A.col(3);
   
    typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> DynamicStride;
   
    // Example 1:  Column of a row-major matrix mapped as a column-major matrix
    // 10 elements between two consecutive elements in a row
    // 50 elements between two consecutive elements in a column
    Eigen::Map<Eigen::MatrixXd,Eigen::Unaligned, DynamicStride >  column3_as_2x5(column3.data(),2,5, DynamicStride(10,50));
       
   
    // Example 1:  Column of a row-major matrix mapped as a column-major matrix
    // 50 elements between two consecutive elements in a column
    // 10 elements between two consecutive elements in a row
    Eigen::Map<MatrixXdrm,Eigen::Unaligned, DynamicStride >  column3_as_2x5_rm(column3.data(),2,5, DynamicStride(50,10));
   
    A.setRandom();
   
   
    for(unsigned int i=0; i<10; i++) {
        for(unsigned int j=0; j<10; j++) {
            A(i,j) = static_cast<double>(i*j);
        }
    } 
    A.row(0).fill(100);

    std::cout << A << std::endl;
   
    std::cout << "Column 3 as a 2x5 is: " << std::endl << column3_as_2x5 << std::endl;
    std::cout << "Column 3 as a 2x5 (row-major) is: " << std::endl << column3_as_2x5_rm << std::endl;
   
    return 0;
}


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]