Hi,
I'm working with (large) N-D matrices and need to sum elements along their dimensions, according to grouping index vectors. To exemplify
A is a 4-D matrix with dimensions (3,4,2,7), andx1 = (2 1 2 3) <- same length as 2nd dimension in Ax2 = (3 3 2 1 3 2 4) <- same length as 4th dimension in A
I would like to find a formula f to sum the matrix along dimension 2 using the grouping variable x1 and along dimension 4 using the grouping variable x2. In the case of x2, for instance, the first, second and fifth 3x4x2 hyperplanes (group "3") should be summed together, as should the third and sixth (group "2"), while groups "1" and "4" should not change. Whether through a one-liner or a loop through the two dimensions involved, the final result should be a matrix of dimension (3,3,2,4).
I've seen similar cases using accumarray and/or arrayfun, but only applied to special (and straightforward) 2-D or 3-D (<- flattened to 2-D in the solutions proposed) matrices.
Is there a generalized (matrix of any dimension) and efficient way, through those or other functions, to obtain the result above?
Greateful in advance for any solution or lead you could provide.
Kind regards
Dan
Best Answer