Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Cell array element-wise operations
Replies: 3   Last Post: Nov 11, 2012 11:16 PM

 Messages: [ Previous | Next ]
 Doug Schwarz Posts: 1,409 Registered: 12/7/04
Re: Cell array element-wise 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", element-wise, 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.

Date Subject Author
11/10/12 Rohan
11/11/12 Nasser Abbasi
11/11/12 Bruno Luong
11/11/12 Doug Schwarz