Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorizing matrix multiplication
Date: Fri, 17 Jun 2011 22:52:19 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 25
Message-ID: <itglr3$lmf$1@newscl01ah.mathworks.com>
References: <itcuh5$jfa$1@newscl01ah.mathworks.com> <itff70$ptf$1@newscl01ah.mathworks.com> <itfuee$ddh$1@newscl01ah.mathworks.com> <itg5qp$6h6$1@newscl01ah.mathworks.com> <itghho$bck$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1308351139 22223 172.30.248.47 (17 Jun 2011 22:52:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 17 Jun 2011 22:52:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:732505

"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