Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
vectorizing matrix multiplication

Subject: vectorizing matrix multiplication

From: Nate

Date: 16 Jun, 2011 12:56:05

Message: 1 of 10

I am trying to vectorize some matrix multiplication.

How would I vectorize this,

a = rand(10,2);
b = rand(10,6);

for i1 = 1:size(a,2)
    for i2 = 1:size(b,2)
        c{i1,i2} = a(:,i1).*b(:,i2);
    end
end

Thanks,

Nate

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 16 Jun, 2011 23:01:20

Message: 2 of 10

"Nate" wrote in message <itcuh5$jfa$1@newscl01ah.mathworks.com>...
> I am trying to vectorize some matrix multiplication.
>
> How would I vectorize this,
>
> a = rand(10,2);
> b = rand(10,6);
>
> for i1 = 1:size(a,2)
> for i2 = 1:size(b,2)
> c{i1,i2} = a(:,i1).*b(:,i2);
> end
> end
>
> Thanks,
>
> Nate
- - - - - - - -
  Hint: You used just the right phrasing in your subject line. Follow that advice and you will have vectorized it.

Roger Stafford

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 16 Jun, 2011 23:22:20

Message: 3 of 10

"Roger Stafford" wrote in message <ite200$81t$1@newscl01ah.mathworks.com>...
> Hint: You used just the right phrasing in your subject line. Follow that advice and you will have vectorized it.
>
> Roger Stafford
- - - - - - - - - -
  My apologies. I didn't look at your question carefully enough. Forget that hint I gave you.

Roger Stafford

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 17 Jun, 2011 00:17:05

Message: 4 of 10

"Nate" wrote in message <itcuh5$jfa$1@newscl01ah.mathworks.com>...
> I am trying to vectorize some matrix multiplication.
>
> How would I vectorize this,
>
> a = rand(10,2);
> b = rand(10,6);
>
> for i1 = 1:size(a,2)
> for i2 = 1:size(b,2)
> c{i1,i2} = a(:,i1).*b(:,i2);
> end
> end
>
> Thanks,
>
> Nate
- - - - - - - -
  Something like this ought to work. However, it might not be faster than your for loop.

 [n,p] = size(a);
 q = size(b,2);
 [I,J] = ndgrid(1:p,1:q);
 C = num2cell(reshape(a(:,I).*b(:,J),n,p,q),1);

Roger Stafford

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 17 Jun, 2011 06:03:02

Message: 5 of 10

"Roger Stafford" wrote in message <ite6e0$ib6$1@newscl01ah.mathworks.com>...
> [n,p] = size(a);
> q = size(b,2);
> [I,J] = ndgrid(1:p,1:q);
> C = num2cell(reshape(a(:,I).*b(:,J),n,p,q),1);
- - - - - - - - - - -
  On second thought that should be:

 [n,p] = size(a);
 q = size(b,2);
 [I,J] = ndgrid(1:p,1:q);
 C = num2cell(reshape((a(:,I).*b(:,J)).',p,q,n),3);

Roger Stafford

Subject: vectorizing matrix multiplication

From: Matt J

Date: 17 Jun, 2011 11:53:05

Message: 6 of 10

"Nate" wrote in message <itcuh5$jfa$1@newscl01ah.mathworks.com>...
> I am trying to vectorize some matrix multiplication.
>
> How would I vectorize this,
>
> a = rand(10,2);
> b = rand(10,6);
>

b=reshape(b,10,1,6);
c=bsxfun(@times,a,b);

In principle, you're done at this point unless you really insist on the data being organized into a cell array (why would you??). If so, you can do

c=pemute( num2cell(c,1) , [2 3 1] );

but this uses an intrinsic for-loop.

Subject: vectorizing matrix multiplication

From: Nate

Date: 17 Jun, 2011 16:13:02

Message: 7 of 10

"Matt J" wrote in message <itff70$ptf$1@newscl01ah.mathworks.com>...
> "Nate" wrote in message <itcuh5$jfa$1@newscl01ah.mathworks.com>...
> > I am trying to vectorize some matrix multiplication.
> >
> > How would I vectorize this,
> >
> > a = rand(10,2);
> > b = rand(10,6);
> >
>
> b=reshape(b,10,1,6);
> c=bsxfun(@times,a,b);
>
> In principle, you're done at this point unless you really insist on the data being organized into a cell array (why would you??). If so, you can do
>
> c=pemute( num2cell(c,1) , [2 3 1] );
>
> but this uses an intrinsic for-loop.

Thank you very much Roger and Matt, that's exactly what I was looking for.

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 17 Jun, 2011 18:19:05

Message: 8 of 10

"Nate" wrote in message <itfuee$ddh$1@newscl01ah.mathworks.com>...
> Thank you very much Roger and Matt, that's exactly what I was looking for.
- - - - - - - - - -
  It might be more efficient to do the permute/transpose first on the smaller arrays:

 [n,p] = size(a); q = size(b,2);
 C = num2cell(bsxfun(@times,reshape(a.',p,1,n),reshape(b.',1,q,n)),3);

(using Matt's bsxfun method.)

Roger Stafford

Subject: vectorizing matrix multiplication

From: Nate

Date: 17 Jun, 2011 21:39:04

Message: 9 of 10

"Roger Stafford" wrote in message <itg5qp$6h6$1@newscl01ah.mathworks.com>...
> "Nate" wrote in message <itfuee$ddh$1@newscl01ah.mathworks.com>...
> > Thank you very much Roger and Matt, that's exactly what I was looking for.
> - - - - - - - - - -
> It might be more efficient to do the permute/transpose first on the smaller arrays:
>
> [n,p] = size(a); q = size(b,2);
> C = num2cell(bsxfun(@times,reshape(a.',p,1,n),reshape(b.',1,q,n)),3);
>
> (using Matt's bsxfun method.)
>
> Roger Stafford

Thanks for the input Roger, but I'm not sure how your method is more efficient?

I used this code to compare your method and Matt's method for one evaluation,

f = zeros(1000,1);
g = zeros(1000,1);
for k = 1:10000
    a = rand(10,2);
    b = rand(10,6);
    
    tic;
    c = reshape(b,size(b,1),1,size(b,2));
    d = bsxfun(@times,a,c);
    f(k) = toc;
    
    tic;
    [n,p] = size(a);
    q = size(b,2);
    e = bsxfun(@times,reshape(a.',p,1,n),reshape(b.',1,q,n));
    g(k) = toc;
end

mean(f)
mean(g)

and for the second, I set,

a = rand(100000,10);
b = rand(100000,10);

the results I got for the first were,

mean(f) = 2.6303e-005
mean(g) = 3.3236e-005

and for the second I got,

mean(f) = 0.1220
mean(g) = 0.1437

Am I doing something incorrect?

Nate

Subject: vectorizing matrix multiplication

From: Roger Stafford

Date: 17 Jun, 2011 22:52:19

Message: 10 of 10

"Nate" wrote in message <itghho$bck$1@newscl01ah.mathworks.com>...
> Thanks for the input Roger, but I'm not sure how your method is more efficient?
> I used this code to compare your method and Matt's method for one evaluation,
>
> f = zeros(1000,1);
> g = zeros(1000,1);
> for k = 1:10000
> a = rand(10,2);
> b = rand(10,6);
> tic;
> c = reshape(b,size(b,1),1,size(b,2));
> d = bsxfun(@times,a,c);
> f(k) = toc;
> tic;
> [n,p] = size(a);
> q = size(b,2);
> e = bsxfun(@times,reshape(a.',p,1,n),reshape(b.',1,q,n));
> g(k) = toc;
> end
- - - - - - - - -
  Your test results are not very surprising. Here you are just testing the added times it takes to do two transposes and one additional reshape in the second code. My reason for the suggestion was that it might be better to transpose the smaller arrays than to wait for a later permute performed on the much larger cell array, but you haven't included this permute step in your testing.

  I would also suggest that you should test it for much larger values of p and q than 2 and 6 to see the effect of the different kinds of transpose/permute.

Roger Stafford

Tags for this Thread

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.

Contact us