Thread Subject: product 3D matrix with a vector

Subject: product 3D matrix with a vector

From: Paolo

Date: 2 Jan, 2012 13:12:09

Message: 1 of 8

Hello,

I have 1 3D matrix like this
A(n,m,k)
and 1 vector like this
V(m,1)

what I am trying to do is the sum of the products of each 2D matrix A(n,m) that I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).

Is it possible to do it in one single command using <sum> ?
I mean without the for..end loop.

Thanks again
P

Subject: product 3D matrix with a vector

From: Nasser M. Abbasi

Date: 2 Jan, 2012 13:30:55

Message: 2 of 8

On 1/2/2012 7:12 AM, Paolo wrote:
> Hello,
>
> I have 1 3D matrix like this
> A(n,m,k)
> and 1 vector like this
> V(m,1)
>
> what I am trying to do is the sum of the products of each 2D matrix A(n,m) that
>I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).
>
> Is it possible to do it in one single command using<sum> ?
> I mean without the for..end loop.
>
> Thanks again
> P


may be:

%data
clear all
A=rand(2,2,2);
b=rand(2,1);

%engine
----------------------------
sum(cell2mat(arrayfun(@(i) A(:,:,i)*b,1:size(A,3),'UniformOutput',false)),2)
----------------------------

%verify
A(:,:,1)*b+A(:,:,2)*b


--Nasser

Subject: product 3D matrix with a vector

From: Paolo

Date: 2 Jan, 2012 13:48:08

Message: 3 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jdsbie$6cq$1@speranza.aioe.org>...
> On 1/2/2012 7:12 AM, Paolo wrote:
> > Hello,
> >
> > I have 1 3D matrix like this
> > A(n,m,k)
> > and 1 vector like this
> > V(m,1)
> >
> > what I am trying to do is the sum of the products of each 2D matrix A(n,m) that
> >I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).
> >
> > Is it possible to do it in one single command using<sum> ?
> > I mean without the for..end loop.
> >
> > Thanks again
> > P
>
>
> may be:
>
> %data
> clear all
> A=rand(2,2,2);
> b=rand(2,1);
>
> %engine
> ----------------------------
> sum(cell2mat(arrayfun(@(i) A(:,:,i)*b,1:size(A,3),'UniformOutput',false)),2)
> ----------------------------
>
> %verify
> A(:,:,1)*b+A(:,:,2)*b
>
>
> --Nasser

Perfect!
Thanks Nasser

P

Subject: product 3D matrix with a vector

From: James Tursa

Date: 2 Jan, 2012 19:32:08

Message: 4 of 8

"Paolo " <tarpanelli@libero.it> wrote in message <jdsaf8$3pp$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have 1 3D matrix like this
> A(n,m,k)
> and 1 vector like this
> V(m,1)
>
> what I am trying to do is the sum of the products of each 2D matrix A(n,m) that I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).
>
> Is it possible to do it in one single command using <sum> ?
> I mean without the for..end loop.
>
> Thanks again
> P

sum(mtimesx(A,V),3)

You can find mtimesx on the FEX here:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

James Tursa

Subject: product 3D matrix with a vector

From: Matt J

Date: 2 Jan, 2012 19:49:09

Message: 5 of 8

"Paolo " <tarpanelli@libero.it> wrote in message <jdsaf8$3pp$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have 1 3D matrix like this
> A(n,m,k)
> and 1 vector like this
> V(m,1)
>
> what I am trying to do is the sum of the products of each 2D matrix A(n,m) that I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).
==============

Another possibility:

result = KronProd({1,V', ones(1,k)},[1,2,3],size(A)) * A;


which uses this:

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation

Subject: product 3D matrix with a vector

From: Matt J

Date: 2 Jan, 2012 19:57:08

Message: 6 of 8

"Paolo " <tarpanelli@libero.it> wrote in message <jdsaf8$3pp$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have 1 3D matrix like this
> A(n,m,k)
> and 1 vector like this
> V(m,1)
>
> what I am trying to do is the sum of the products of each 2D matrix A(n,m) that I can extract from the 3D matrix A(n,m,k) with the vector V(m,1).
================



Come to think of it, the optimal method is probably this:

result= sum(A,3)*V

Subject: product 3D matrix with a vector

From: Nasser M. Abbasi

Date: 2 Jan, 2012 21:05:14

Message: 7 of 8

On 1/2/2012 1:57 PM, Matt J wrote:
> "Paolo "<tarpanelli@libero.it> wrote in message<jdsaf8$3pp$1@newscl01ah.mathworks.com>...
>> Hello,
>>
>> I have 1 3D matrix like this
>> A(n,m,k)
>> and 1 vector like this
>> V(m,1)
>>
  ================
>
>
>
> Come to think of it, the optimal method is probably this:
>
> result= sum(A,3)*V

yes ofocurse ! good.

--Nasser

Subject: product 3D matrix with a vector

From: Matt J

Date: 3 Jan, 2012 19:55:09

Message: 8 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jdt667$8to$1@speranza.aioe.org>...
>
> >
> > Come to think of it, the optimal method is probably this:
> >
> > result= sum(A,3)*V
>
> yes ofocurse ! good.
=====================

It might just be worth mentioning that, once k gets sufficiently large, I do see James' MTIMESX approach start to overtake the above approach in performance:


n=150; m=150; k=2000;

A=rand(n,m,k);
V=rand(m,1);


tic;
res1 = sum(mtimesx(A,V),3);
toc;
%Elapsed time is 0.074715 seconds.


tic;
res2 = sum(A,3)*V;
toc;
%Elapsed time is 0.078182 seconds.



This is a little counter-intuitive for me because res2 is computed with far fewer multiplies, which I always thought to be more time consuming than adds. But there you have it...

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mtimesx Matt J 3 Jan, 2012 15:13:32
kronprod Matt J 3 Jan, 2012 15:13:32
rssFeed for this Thread

Contact us at files@mathworks.com