help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multiplying matrices into tensors?


From: Jordi Gutiérrez Hermoso
Subject: Re: multiplying matrices into tensors?
Date: Thu, 4 Aug 2011 13:07:35 -0500

On 4 August 2011 01:48, James Cloos <address@hidden> wrote:
>>>>>> "JGH" == Jordi Gutiérrez Hermoso <address@hidden> writes:
>
> JGH> I'm not sure if I understand, but I imagine you want something
> JGH> like this:
>
> JGH>      c = bsxfun(@times, permute(a,[3,2,1]), permute(b,[2,3,1]))
>
> I have to confirm each element, but, based on the above, it looks
> like
>
>  c = bsxfun(@times, permute(a,[1,2,3]), permute(b,[1,3,2]))
>
> might have been the solution, but testing reminded me that the
> multiplication needs to be done a bit differently:
>
>  c(1,u,v) = a(1,u) * b(4,v) + b(1,v) * a(4,u)
>  c(2,u,v) = a(2,u) * b(4,v) + b(2,v) * a(4,u)
>  c(3,u,v) = a(3,u) * b(4,v) + b(3,v) * a(4,u)
>  c(4,u,v) = a(4,u) * b(4,v)

This can still be done with bsxfun, but takes one more line:

     c = bsxfun(@times, permute(a, [1,2,3]), permute(b(4,:),[3,1,2]))
     c(1:3,:,:) +=  bsxfun(@times, permute(b(1:3,:), [1,3,2]),
                                   permute(a(4,:), [1,2,3]))

Note that permute(..., [1,2,3]) is superfluous. I just put in place to
make it explicit how the indices of a and b correspond to the indices
of c.

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]