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: Ulrik Nash

Date: 6 Sep, 2010 13:44:05

Message: 1 of 5

Hi Everyone,

Suppose I have a matrix A:

A = [1 2;3 4;5 6]

and B:

B = [1 2]

What I would like is to form a matrix C, by multiplying the first column in A by the first column in B, and the second column in A by the second column i B to obtain:

C = [1 4;3 8;5 12]

I have tried C = B.*A but this does not work, because the dimensions do not match. But is there not a simple operation, that allows me to form C?

Regards,

Ulrik.

Subject: Matrix multiplication

From: Matt J

Date: 6 Sep, 2010 14:18:22

Message: 2 of 5

bsxfun(@times, A,B)

Subject: Matrix multiplication

From: Ulrik Nash

Date: 6 Sep, 2010 14:23:05

Message: 3 of 5

Thank you!

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i62t7e$dji$1@fred.mathworks.com>...
> bsxfun(@times, A,B)

Subject: Matrix multiplication

From: Steve Amphlett

Date: 6 Sep, 2010 14:28:05

Message: 4 of 5

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i62t7e$dji$1@fred.mathworks.com>...
> bsxfun(@times, A,B)

Or in old money:

A.*repmat(B,3,1)

Or in ancient money:

A.*B(ones(1,3),:)

Subject: Matrix multiplication

From: Matt J

Date: 6 Sep, 2010 14:35:05

Message: 5 of 5

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <i62tpl$jou$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i62t7e$dji$1@fred.mathworks.com>...
> > bsxfun(@times, A,B)
>
> Or in old money:
>
> A.*repmat(B,3,1)
>
> Or in ancient money:
>
> A.*B(ones(1,3),:)
==============

For a small number of columns, like in this example, I'm pretty sure a for-loop is still the fastest.

C=A;
for ii=1:2
  C(:,ii)=A(:,ii).*B(:,ii);
end

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