user1214549
user1214549

Reputation: 13

MATLAB: Multiply each column in Matrix A by a row in Matrix B

Matrix A: 10 rows, 50 columns

a1 a2 a3 ....

b1 b2 b3 ....

c1 c2 c3 .... ...

Matrix B: 50 rows, 10 columns

x1 x2 x3....

y1 y2 y3....

....

I would like to multiply data in each rows in Matrix A by columns(1) in Matrix B, the results will look like:

[x1*a1, x2*b1, x3*c1, x4*d1....

x1*a2, x2*b2, x3*c2, x4*d2....

x1*.

....]

Then, multiply each rows of Matrix A by columns(2) in Matrix 8:

[y1*a1, y2*b1, y3*c1, y4*d1....

y1*a2, y2*b2, y3*c2, y4*d2....

y1*. ....]

Then, by row(3) till columns(50)

I am looking for a script : )

Upvotes: 1

Views: 3203

Answers (1)

Oli
Oli

Reputation: 16035

You can use bsxfun.

A=rand(10,50);
B=rand(50,10);
C=bsxfun(@times,A,permute(B,[3 1 2]));

Here C(:,:,1) would be your first result matrix, and C(:,:,2) the second...

Upvotes: 5

Related Questions