
Re: Cell array elementwise operations
Posted:
Nov 11, 2012 11:16 PM


In article <k7n744$b1a$1@newscl01ah.mathworks.com>, "Rohan " <rps2689@gmail.com> wrote:
> I have 2 cell arrays. Each cell array is 2D and has elements which are 2D > matrices themselves. So I have a 2D array of 2D matrices, stored in the form > of a cell array. > > I want to "add", elementwise, each element of each matrix with its > corresponding element in the corresponding matrix in the other cell array. > > Is it possible to do this in one line in matlab? '+' operator and bsxfun > clearly dont work, even though, logically speaking, bsxfun should do the > trick for me.
If A and B are your 2 cell arrays:
C = cellfun(@(x,y){x+y},A,B);
will do it. The reason why bsxfun doesn't do what you want is that it would perform
C(1,1) = A(1,1) + B(1,1);
but those are cells. You would need it to operate on the contents of the cells, i.e.,
C{1,1} = A{1,1} + B{1,1};
but it doesn't do that.
 Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address.

