Search code examples
arraysmatlabvectorizationcell-array

Elementwise sum of matrices in cell array


I have a cell array as follows:

A = { [1 2;3 4] ;[5 6;7 8]};

How can I add

A{1,1} + A{2,1} = {[6 8;10 12]};

without a loop in Matlab?


Solution

  • If the cell array has size 2, you can simply do:

    result = plus(A{:});
    

    This calls plus (addition) on the comma-separated list generated from the two-element cell array.