
Re: Using @times or @prod with cellfun
Posted:
Feb 21, 2013 10:17 AM


"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

