Search code examples
arraysmatlabsumcellcell-formatting

Summing arrays inside cells


I have a code in MATLAB that produces 1000 x 1000 arrays in a loops that runs ten times; to try and save all these loops, I commit these arrays to a cell; Now I have a cell 1 x 10 cell nalled PL, each element being a 1000 x 1000 array; I want to sum these in the easiest way possible, so that I get one 1000 x 1000 output. I've tried using

PLtot = cellfun(@sum,PL, 'UniformOutput',false);

but this does not work at all for me - any ideas? I'm sure this should be simple but having a headache doing it!


Solution

  • Instead of storing your array into cellarray.

    Just add an extra dimension to your initial array. Always preallocate the size to the array.

    PL = zeros(1000,1000,10);
    

    So when you want to access to each image it's easier. And for the sum just do :

    PLtot = sum(PL,3);