Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Doug Schwarz

Posts: 1,386
Registered: 12/7/04
Re: Cell array element-wise operations
Posted: Nov 11, 2012 11:16 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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
Make obvious changes to get real email address.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum 1994-2015. All Rights Reserved.