Registered Member
|
Hi,
I have a sample matrix A(6,4), which looks like following: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 I would like to reduce it into a B(3,4) matrix by summing up 2 consecutive elements (could be 3 or 4, ... consecutive elements) in every column, the new matrix should then looks like this: 2 2 2 2 2 2 2 2 2 2 2 2 In math notation: B(i,j) = A(i*2,j) + A(i*2+1,j) How do I do this in an efficient way ? This reduction operation is part of an expression in my program Thanks ! |
Registered Member
|
If your matrix is stored in row-major order, that's not too hard: something like
for(result_row) result.row(result_row) = src.row(2*result_row) + src.row(2*result_row+1); If your matrix is column major, that's trickier, I think you would really need to code your own expression class.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
Thanks for the quick answer !
Row-major or Column-Major is actually not really a problem for me, since I can have my matrix stored in either way. This is my situation: I have 3 arrays (since I only do coefficient-wise operations) and have to compute the following expression: A(n,m) *= (B(n*4,m) * C(n*4,m)).reduce() * A(n,m) (*) n,m is the dimension of the array and reduce() is the reduction operation that I just mentioned above. 4 is the number of consecutive elements that need to be summed up Of course I can use a temporary matrix and for loop (like what you have suggested above) to do the reduction but I think it is inefficient, isn't it ? My matrice are very big and (*) is carried out thousands times ... |
Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient