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:
matrix multiplication

Subject: matrix multiplication

From: nick

Date: 28 Jun, 2010 01:12:20

Message: 1 of 8

Hi all,

Could someone please give me a hand for this simple question?

I want to multiply colum by colum of the two matrices.

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:

Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]

Thanks heap for your inputs.

Nick

Subject: matrix multiplication

From: Faraz Afzal

Date: 28 Jun, 2010 01:37:05

Message: 2 of 8

nick <nghiango79@gmail.com> wrote in message <1124078130.14069.1277687570117.JavaMail.root@gallium.mathforum.org>...
> Hi all,
>
> Could someone please give me a hand for this simple question?
>
> I want to multiply colum by colum of the two matrices.
>
> 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:
>
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]
>
> Thanks heap for your inputs.
>
> Nick

Hey Nick

To my knowledge there is none.
You can make use of a for loop and save it in an mfile , therby u can achieve it in one command.

You know how to generalize it with for loop..don't you ?
Regards
Faraz

Subject: matrix multiplication

From: Roger Stafford

Date: 28 Jun, 2010 01:46:05

Message: 3 of 8

nick <nghiango79@gmail.com> wrote in message <1124078130.14069.1277687570117.JavaMail.root@gallium.mathforum.org>...
> Hi all,
>
> Could someone please give me a hand for this simple question?
>
> I want to multiply colum by colum of the two matrices.
>
> 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:
>
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)]
>
> Thanks heap for your inputs.
>
> Nick

 Z = bsxfun(@times,X,Y);

Roger Stafford

Subject: matrix multiplication

From: nick

Date: 28 Jun, 2010 03:21:02

Message: 4 of 8

This is what I am looking for. Thanks very much Roger.

Subject: matrix multiplication

From: Steve Amphlett

Date: 28 Jun, 2010 08:52:05

Message: 5 of 8

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.

Before bsxfun there was repmat.
Before repmat there was ones.

Subject: matrix multiplication

From: us

Date: 28 Jun, 2010 10:21:20

Message: 6 of 8

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <i09nrl$t22$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.
>
> Before bsxfun there was repmat.
> Before repmat there was ones.
 
Before ones there was eval.
And eval was evil.

us

Subject: matrix multiplication

From: Andy

Date: 28 Jun, 2010 13:17:22

Message: 7 of 8

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

Subject: matrix multiplication

From: John D'Errico

Date: 28 Jun, 2010 13:34:07

Message: 8 of 8

"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

Tags for this Thread

No tags are associated with 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