Vectors of the sums for an array of arbitrary dimensions.

조회 수: 3 (최근 30일)
Michael Ransom
Michael Ransom 2019년 3월 20일
편집: Michael Ransom 2019년 3월 21일
I require the vectors of sums for the sub-arrays along an array of arbitrary dimensions. The following question is related to the topic on arrays of arbitrary dimensions posted here.
Given a j-dimensional regular array:
j = 2; % example value
d = 5; % example value
rng('default'); % reproduces rand
Array = rand( repmat( d , 1 , j ) ) ; % random element values
I require the vectors of sum for subarrays along the dimensions of , followed by subsubarrays, etc.
The simple example of matrix is:
ArraySum = cell( 2 , 1 ) ;
for m = 1:2
ArraySum{ m } = reshape( sum( Array , m ) , [] , 1 ) ;
end
where is a cell array containing two vectors for the sums across each of the respective dimensions of .
The desired function for the case is:
ArraySum = cell( 3 , 1 ) ;
ArraySumArray = [];
for m = 1:3
for n = 1:2
ArraySum{ m }{ n } = round( reshape( sum( reshape( sum( Array , m ) , d , d ) , n ) , d , 1 ) , 10 ) ; % round needed to remove repeating vectors; reshape ensures vectors may concatenate
ArraySumArray = [ ArraySumArray ArraySum{ m }{ n } ] ; % unique() not supported for rows of cell arrays
end
end
ArraySumArray = unique(ArraySumArray','rows')' ; % remove repeating vectors
for m = 1:3
ArraySum{ m } = ArraySumArray( : , m ) ; % overwrite repeated vectors
end
where now contains three vectors, asis three-dimensional. Note that cases for must account for the loss in dimensionality with each result of the function, where
.
There are also repeating vectors with each valid combination of sums that must also be removed. This is dealt with using the function.
The problem now extends to all cases, where a j-dimensional regular array yields j unique vectors of sums in the manner desribed above. This implies the pseudo-code structure, albeit ineloquently:
ArraySum = cell( j , 1 ) ;
ArraySumArray = [];
for m(1)) = 1:j
...
for m(j-1) = 1:2
ArraySum{ m(1) }...{ m{ j-1 } } = round( reshape( sum( ... reshape( sum( Array , m(1) ) , repmat( d , 1 , j - 1 ) ) , ... , m(j-1) ) , d , 1 ) , 10 ) ; % round needed to remove repeating vectors; reshape ensures vectors may concatenate
ArraySumArray = [ ArraySumArray ArraySum{ m(1) }...{ m(j-1) } ] ; % unique() not supported for rows of cell arrays
end
...
end
ArraySumArray = unique(ArraySumArray','rows')' ; % remove repeating vectors
for m = 1:j
ArraySum{ m } = ArraySumArray( : , m ) ; % overwrite repeated vectors
end
Does there exist a valid means of obtaining the vectors of sums in the manner described above for an array of arbitrary dimensions in MATLAB?
  댓글 수: 1
Michael Ransom
Michael Ransom 2019년 3월 21일
I have managed to come up with a solution that fits my needs.
I have overcomplicated a relatively simple problem, which essentially boils down to summing over all dimensions of the array with the exception of one element from the same list:
function ArraySum = sumNDarray( j , A )
for m = 1:j
Array = A ;
jlist = 1:j;
jlist( m ) = [];
SumIndex{ m } = jlist;
for n = 1:j-1
Array = sum( Array , SumIndex{ m }( n ) , 'omitnan' ) ;
end
ArraySum{ m } = reshape( Array , [] , 1 ) ;
end

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

채택된 답변

Steven Lord
Steven Lord 2019년 3월 21일
I have overcomplicated a relatively simple problem, which essentially boils down to summing over all dimensions of the array with the exception of one element from the same list
What release of MATLAB are you using? If you're using release R2018b or later, use the vector dimension argument functionality introduced in that release. Let's define a sample 4-dimensional array to start.
sz = [2 3 4 5];
n = numel(sz);
A = reshape(1:prod(sz), sz);
Define a vector containing all the dimensions, and preallocate a cell array to hold the results of summing in all dimensions but one for each of the dimensions of A.
dims = 1:n;
results = cell(1, n);
Now let's do the summation.
for excludedDim = 1:n
D = dims;
D(excludedDim) = [];
results{excludedDim} = sum(A, D);
end
If you look at each element of results, it has the same size as A in the dimension that was excluded from summation and size 1 in all the others. This does mean that some of the elements in results have fewer dimensions than A does due to trailing singleton dimensions being dropped.
  댓글 수: 1
Michael Ransom
Michael Ransom 2019년 3월 21일
편집: Michael Ransom 2019년 3월 21일
Thanks for your answer, Steven Lord.
I checked your code with my example. The results produced by both solutions are identical.
A quick follow up: Are there any particular merits to using the new functionality presented in your solution when compared to mine?
Many thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by