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

Rebinning a matrix

Tags: None
(comma "," separated)
KnightCoder
Registered Member
Posts
1
Karma
0

Rebinning a matrix

Wed Jan 07, 2015 2:05 pm
I would like to rebin a matrix by summing sub-matrices. For example the 4x4 matrix
Code: Select all
ArrayXXd A(4,4);
A <<   1,  2,  5,  6,
       3,  4,  7,  8,
       9, 10, 13, 14,
      11, 12, 15, 16;

into the 2x2 matrix
Code: Select all
ArrayXXd B(2,2);
B <<     1+2+3+4,     5+6+7+8,
      9+10+11+12, 13+14+15+16;


One way of doing this, could be using blocks:
Code: Select all
unsigned int blockSize = 2;
for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; j++) {
        B(i,j) = A.block(i*blockSize,j*blockSize, blockSize, blockSize).sum();
    }
}

This still loops over all elements of B, though, which makes it not very fast. Does anyone know a way to make this reduction more efficient?

EDIT.
As a follow-up: not using blocks but using straightforwardly A(.,.) is a bit slower but not much slower.
Not using an Eigen array but a native double** and using A[.][.] speeds up the execution time with a factor of 10.


Bookmarks



Who is online

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