http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619
MATLAB Central Newsreader  matrix multiplication
Feed for thread: matrix multiplication
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 28 Jun 2010 01:12:20 +0000
matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758008
nick
Hi all,<br>
<br>
Could someone please give me a hand for this simple question?<br>
<br>
I want to multiply colum by colum of the two matrices.<br>
<br>
For example: matrix X has the size (4,3) and matrix Y has the size (1,3). Is there any single Matlab command to produce a matrix Z in the following format:<br>
<br>
Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]<br>
<br>
Thanks heap for your inputs.<br>
<br>
Nick

Mon, 28 Jun 2010 01:37:05 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758010
Faraz Afzal
nick <nghiango79@gmail.com> wrote in message <1124078130.14069.1277687570117.JavaMail.root@gallium.mathforum.org>...<br>
> Hi all,<br>
> <br>
> Could someone please give me a hand for this simple question?<br>
> <br>
> I want to multiply colum by colum of the two matrices.<br>
> <br>
> For example: matrix X has the size (4,3) and matrix Y has the size (1,3). Is there any single Matlab command to produce a matrix Z in the following format:<br>
> <br>
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]<br>
> <br>
> Thanks heap for your inputs.<br>
> <br>
> Nick<br>
<br>
Hey Nick<br>
<br>
To my knowledge there is none.<br>
You can make use of a for loop and save it in an mfile , therby u can achieve it in one command.<br>
<br>
You know how to generalize it with for loop..don't you ?<br>
Regards<br>
Faraz

Mon, 28 Jun 2010 01:46:05 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758011
Roger Stafford
nick <nghiango79@gmail.com> wrote in message <1124078130.14069.1277687570117.JavaMail.root@gallium.mathforum.org>...<br>
> Hi all,<br>
> <br>
> Could someone please give me a hand for this simple question?<br>
> <br>
> I want to multiply colum by colum of the two matrices.<br>
> <br>
> For example: matrix X has the size (4,3) and matrix Y has the size (1,3). Is there any single Matlab command to produce a matrix Z in the following format:<br>
> <br>
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]<br>
> <br>
> Thanks heap for your inputs.<br>
> <br>
> Nick<br>
<br>
Z = bsxfun(@times,X,Y);<br>
<br>
Roger Stafford

Mon, 28 Jun 2010 03:21:02 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758028
nick
This is what I am looking for. Thanks very much Roger.

Mon, 28 Jun 2010 08:52:05 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758059
Steve Amphlett
nick <nghiango79@gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org>...<br>
> This is what I am looking for. Thanks very much Roger.<br>
<br>
Before bsxfun there was repmat.<br>
Before repmat there was ones.

Mon, 28 Jun 2010 10:21:20 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758084
us
"Steve Amphlett" <Firstname.Lastname@WhereIWork.com> wrote in message <i09nrl$t22$1@fred.mathworks.com>...<br>
> nick <nghiango79@gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org>...<br>
> > This is what I am looking for. Thanks very much Roger.<br>
> <br>
> Before bsxfun there was repmat.<br>
> Before repmat there was ones.<br>
<br>
Before ones there was eval.<br>
And eval was evil.<br>
<br>
us

Mon, 28 Jun 2010 13:17:22 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758121
Andy
nick <nghiango79@gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org>...<br>
> This is what I am looking for. Thanks very much Roger.<br>
<br>
What was wrong with your original code? In fact, it was significantly faster:<br>
<br>
X = rand(1000,1000);<br>
Y = rand(1,1000);<br>
<br>
tic;<br>
for i=1:100<br>
Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)];<br>
clear Z;<br>
end<br>
z1 = toc<br>
<br>
tic;<br>
for i =1:100<br>
Z=bsxfun(@times, X,Y);<br>
clear Z;<br>
end<br>
z2 = toc <br>
<br>
% result:<br>
% z1 = 0.0042<br>
% z2 = 1.0881

Mon, 28 Jun 2010 13:34:07 +0000
Re: matrix multiplication
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285619#758128
John D'Errico
"Andy " <theorigamist@gmail.com> wrote in message <i0a7d2$7cv$1@fred.mathworks.com>...<br>
> nick <nghiango79@gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root@gallium.mathforum.org>...<br>
> > This is what I am looking for. Thanks very much Roger.<br>
> <br>
> What was wrong with your original code? In fact, it was significantly faster:<br>
> <br>
> X = rand(1000,1000);<br>
> Y = rand(1,1000);<br>
> <br>
> tic;<br>
> for i=1:100<br>
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)];<br>
> clear Z;<br>
> end<br>
> z1 = toc<br>
> <br>
> tic;<br>
> for i =1:100<br>
> Z=bsxfun(@times, X,Y);<br>
> clear Z;<br>
> end<br>
> z2 = toc <br>
> <br>
> % result:<br>
> % z1 = 0.0042<br>
> % z2 = 1.0881<br>
<br>
DUH. You are kidding of course?????????<br>
<br>
Maybe, JUST maybe, if you compared apples to apples, you<br>
might have gotten a sane response.<br>
<br>
You do understand that your first test multiplies only THREE<br>
columns of X and Y. The call to bsxfun multiplies EVERY<br>
column of X with the corresponding element in Y.<br>
<br>
There is a difference.<br>
<br>
And of course, you might try writing out EVERY column.<br>
<br>
Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3) X(:,4)*Y(4) ...<br>
X(:,5)*Y(5) X(:,6)*Y(6) X(:,7)*Y(7) X(:,8)*Y(8) ...<br>
X(:,9)*Y(9) X(:,10)*Y(10) X(:,11)*Y(11) X(:,12)*Y(12) ...<br>
...];<br>
<br>
Ok, I got bored with with it after 12 columns. Maybe you<br>
have a higher tolerance for boredom.<br>
<br>
John