Path: news.mathworks.com!not-for-mail From: "John D'Errico" <woodchips@rochester.rr.com> Newsgroups: comp.soft-sys.matlab Subject: Re: matrix multiplication Date: Mon, 28 Jun 2010 13:34:07 +0000 (UTC) Organization: John D'Errico (1-3LEW5R) Lines: 49 Message-ID: <i0a8cf$c30$1@fred.mathworks.com> References: <i08ust$ip9$1@fred.mathworks.com> <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org> <i0a7d2$7cv$1@fred.mathworks.com> Reply-To: "John D'Errico" <woodchips@rochester.rr.com> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1277732047 12384 172.30.248.35 (28 Jun 2010 13:34:07 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 28 Jun 2010 13:34:07 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 869215 Xref: news.mathworks.com comp.soft-sys.matlab:648637 "Andy " <theorigamist@gmail.com> wrote in message <i0a7d2$7cv$1@fred.mathworks.com>... > nick <nghiango79@gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org>... > > This is what I am looking for. Thanks very much Roger. > > What was wrong with your original code? In fact, it was significantly faster: > > X = rand(1000,1000); > Y = rand(1,1000); > > tic; > for i=1:100 > Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]; > clear Z; > end > z1 = toc > > tic; > for i =1:100 > Z=bsxfun(@times, X,Y); > clear Z; > end > z2 = toc > > % result: > % z1 = 0.0042 > % z2 = 1.0881 DUH. You are kidding of course????????? Maybe, JUST maybe, if you compared apples to apples, you might have gotten a sane response. You do understand that your first test multiplies only THREE columns of X and Y. The call to bsxfun multiplies EVERY column of X with the corresponding element in Y. There is a difference. And of course, you might try writing out EVERY column. Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3) X(:,4)*Y(4) ... X(:,5)*Y(5) X(:,6)*Y(6) X(:,7)*Y(7) X(:,8)*Y(8) ... X(:,9)*Y(9) X(:,10)*Y(10) X(:,11)*Y(11) X(:,12)*Y(12) ... ...]; Ok, I got bored with with it after 12 columns. Maybe you have a higher tolerance for boredom. John