MATLAB: Find sum of matrices, of different sizes, in a cell

cellcell arraymatrix array

I have n matrices with different sizes in a cell and I need to find the cumulative sum, without using a for loop.
With a for loop, here is my approach
% C = cell containing n matrices
% size(C) = n, 1
total = 0;
for i = 1:n
total = total + sum(sum(C{i}))
end

Best Answer

It's arguable that cellfun is not a loop, but here is one way to do it
C{1}=magic(3);
C{2}=magic(5);
total = cumsum(cellfun(@(c) sum(c(:)), C)) %for cumulative sum
%or
total = sum(cellfun(@(c) sum(c(:)), C)) %equivalent to your example