MATLAB: Summing arrays within a cell array.

cell array addition

I've got a cell array which consists of 501 4D arrays, I want to sum all of these together and output a single 4D array. I've tried using sum and plus but get the error saying too many input arguments.

Best Answer

What about a simple loop?
S = 0;
for k = 1:numel(C)
S = S + C{k};
end
You can check if a vectorized code is faster:
S = sum(cat(5, C{:}), 5);
While it does not have the overhead for the loop, it requires a large temporary array.