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:
Vectorizing multiplication (similar to elementwise)

Subject: Vectorizing multiplication (similar to elementwise)

From: George

Date: 7 Feb, 2009 01:17:02

Message: 1 of 4

Hello,

Is there any way to realize vector-wise multiplication, just as elementwise multiplication does?

In my case, there are two matrices: A1 (M x N), A2 (N X M). I want an M x 1 matrix, with element with an index k being the dot product of vectors A1(k, :) and A2(:, k). That is, the resultant matrix is a vector consisting the values on the diagonal of A1*A2. I do not want to use matrix multiplication followed by a diag() function, as it takes time to computer useless values, neither do I want to use loop. I think there is a way to mimic what we do with elementwise multiplication, which I would call vector-wise multiplication.

Can anybody help me? Thanks.

Subject: Vectorizing multiplication (similar to elementwise)

From: Matt Fig

Date: 7 Feb, 2009 01:30:20

Message: 2 of 4

This uses hidden loops (in C).

a = round(rand(3,2)*8)
b = round(rand(2,3)*8)
bsxfun(@(x,y) dot(x,y),a,b)




,}i>8rng}gr}slf__c_}saw_ckkrmfmj}C}%cmWnf%`}at}_ce_jm_m`Kml

Subject: Vectorizing multiplication (similar to elementwise)

From: Roger Stafford

Date: 7 Feb, 2009 01:38:02

Message: 3 of 4

"George " <georgew666@gmail.com> wrote in message <gminee$o3g$1@fred.mathworks.com>...
> Hello,
>
> Is there any way to realize vector-wise multiplication, just as elementwise multiplication does?
>
> In my case, there are two matrices: A1 (M x N), A2 (N X M). I want an M x 1 matrix, with element with an index k being the dot product of vectors A1(k, :) and A2(:, k). That is, the resultant matrix is a vector consisting the values on the diagonal of A1*A2. I do not want to use matrix multiplication followed by a diag() function, as it takes time to computer useless values, neither do I want to use loop. I think there is a way to mimic what we do with elementwise multiplication, which I would call vector-wise multiplication.
>
> Can anybody help me? Thanks.

  Why not try

 sum(A1.*A2.',2)

if you want a column vector result or

 sum(A1'.*A2,1)

if you want a row vector result.

Roger Stafford

Subject: Vectorizing multiplication (similar to elementwise)

From: George

Date: 7 Feb, 2009 02:01:02

Message: 4 of 4

Thanks. Actually I was inspired from the use of dot() to achieve it in an easier way.

dot(A1, A2', 2)

"George " <georgew666@gmail.com> wrote in message <gminee$o3g$1@fred.mathworks.com>...
> Hello,
>
> Is there any way to realize vector-wise multiplication, just as elementwise multiplication does?
>
> In my case, there are two matrices: A1 (M x N), A2 (N X M). I want an M x 1 matrix, with element with an index k being the dot product of vectors A1(k, :) and A2(:, k). That is, the resultant matrix is a vector consisting the values on the diagonal of A1*A2. I do not want to use matrix multiplication followed by a diag() function, as it takes time to computer useless values, neither do I want to use loop. I think there is a way to mimic what we do with elementwise multiplication, which I would call vector-wise multiplication.
>
> Can anybody help me? Thanks.

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