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 valuerng('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
endendArraySumArray = 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 ...endArraySumArray = unique(ArraySumArray','rows')' ; % remove repeating vectorsfor m = 1:j ArraySum{ m } = ArraySumArray( : , m ) ; % overwrite repeated vectorsend
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?
Best Answer