N-D matrix accumarray

조회 수: 23 (최근 30일)
Daniele Rocchetta
Daniele Rocchetta 2017년 3월 30일
댓글: Daniele Rocchetta 2017년 3월 30일
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), and
x1 = (2 1 2 3) <- same length as 2nd dimension in A
x2 = (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

채택된 답변

Guillaume
Guillaume 2017년 3월 30일
편집: Guillaume 2017년 3월 30일
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]); %demo data
%generate indices in all the dimension, and replace appropriate one with grouping variables:
indices = arrayfun(@(s) 1:s, size(A), 'UniformOutput', false); %vector of indices in each dimension
indices{2} = [2 1 2 3]; %grouping in 2nd dimension
indices{4} = [3 3 2 1 3 2 4]; %grouping in 4th dimension
%calculate cartesian product of indices
[indices{:}] = ndgrid(indices{:});
indices = cell2mat(cellfun(@(v) v(:), indices, 'UniformOutput', false));
%apply accumarray with default sum function
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).
  댓글 수: 3
Guillaume
Guillaume 2017년 3월 30일
With regards to the ordering, yes, that's how accumarray works. To have the result you want:
[~, ~, newgrouping] = unique(grouping, 'stable');
Daniele Rocchetta
Daniele Rocchetta 2017년 3월 30일
That's great thanks! You didn't even leave the last mile for me... Kudos Dan

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 3월 30일
A = randi(10,3,4,2,7);
x1 = [2 1 2 3];
x2 = [3 3 2 1 3 2 4];
s = size(A);
[a,b,c,d] = ndgrid(1:s(1),x1,1:s(3),x2);
out = accumarray([a(:),b(:),c(:),d(:)], A(:));
  댓글 수: 1
Daniele Rocchetta
Daniele Rocchetta 2017년 3월 30일
Hi Andrei and thanks for you help. Unfortunately your algorithm does not have the degree of generality I need, while correct for the exemplified case above. The matrices I work with can have a different number of dimensions, and the dimensions along which to sum are also unknown at the outset. All the best Dan

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by