Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 584
DRG Avatar asked May 14 '26 22:05

DRG


2 Answers

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);
like image 123
Vuwox Avatar answered May 17 '26 11:05

Vuwox


The following works for me:

PLtot =  sum(cat(3,PL{:}),3);

It concatenates the arrays in the 3rd dimension and then simply sums along this dimension.

like image 45
Rich Nicholson Avatar answered May 17 '26 12:05

Rich Nicholson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!