How can I sum over specific dimensions in a n-dimensional array?
Show older comments
I have an array of unknown dimension n. I want to sum the values of the array over the last d dimensions in order to get a new array of dimension n-d. My current code is:
% Determine the number of dimensions n in the array and d
n = ndims(x);
d = n/2;
% sum over the last d dimensions
for i = 1:d
x = sum(x, n+1-d);
end
Is there a way to avoid the for loop and do this in a more efficient way?
Accepted Answer
More Answers (0)
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!