Yes, accumarray will do it easily. You just need to generate the correct subscripts for it, which is the cartesian product of all the indices in all the dimension, replacing the appropriate dimensions with your grouping variables:
A = randi(100, [3 4 2 7]);
indices = arrayfun(@(s) 1:s, size(A), 'UniformOutput', false);
indices{2} = [2 1 2 3];
indices{4} = [3 3 2 1 3 2 4];
[indices{:}] = ndgrid(indices{:});
indices = cell2mat(cellfun(@(v) v(:), indices, 'UniformOutput', false));
B = accumarray(indices, A(:));
Edit: note that the above will work with arrays of any dimension (within reason, ndgrid will run out of memory for too many dimensions).
Best Answer