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: Using @times or @prod with cellfun
Replies: 2   Last Post: Feb 21, 2013 5:20 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Using @times or @prod with cellfun
Posted: Feb 21, 2013 10:17 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"Barry Shaw" <Barry@h.com> wrote in message
news:kg2vo7$lg$1@newscl01ah.mathworks.com...
>
> Hi
>
> I was wondering if some could help. I think it's fairly straightforward.
>

>>> a(:,:,1)={[2 5 5] [5 7 7];[3 8] [2 6]};
>>> a(:,:,3)={[2 5 5] [5 7 7];[3 8] [2 6]};

>
> This give
> I want to multiply for example [2 5 5] and [2 5 5] to get [4 25 25]. I
> want to do this with all 4 elements. I've done this using

>>> a(:,:,2) = cellfun(@times,a(:,:,1),a(:,:,3),'Uni',0);

Yes. This will call TIMES with two inputs, the first one from a(:, :, 1) and
the second the corresponding element of a(:, :, 3).

>>> a{1,1,2}
>
> ans =
>
> 4 25 25
>
> However, I want to generalise this, to multiplying more than 2 planes in
> the 3rd dimension. I've tried

>>> a(:,:,2) = cellfun(@times,a(:,:,[1 3]),'Uni',0)
> ??? Error using ==> times
> Not enough input arguments.


This will call TIMES repeatedly with _one_ input. Each input will be one of
the cells from a(:, :, [1 3]). But TIMES requires exactly two inputs.

I'm not entirely certain what you mean by "multiplying more than 2 planes in
the 3rd dimension." Do you mean you want to compute a.*b.*c where a, b, and
c are all the contents of cells from a(:, :, [1 3])?

> I've also tried cellfun(@(x) prod(x,3), a(:,:,[1 3]), 'UniformOutput', 0).
>
> But these are giving me errors, or not what I want.
>
> Anybody help please?


For the a variable you defined above, what would you expect:

q = cellfun(@times,a(:,:,[1 3]),'Uni',0)

to return? Knowing what you want may help us figure out how to get you
there.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.