erogol
erogol

Reputation: 13614

how to multiply each row with each row of another matrix elementwise in matlab?

I have such a matrix

m1 = [ 1 2 3; 4 5 6; 1 2 3]
m2 = [ 2 2 2];

and I want to multiply each row of m1 with m2 elementwise .

So result is

result = [2 4 6; 8 10 12; 2 4 6]

How would I do it?

Upvotes: 5

Views: 5840

Answers (2)

bazz
bazz

Reputation: 276

You could also use

 result = diag(m2)*m1;

Upvotes: 2

Nasser
Nasser

Reputation: 13131

 bsxfun(@times,m1,m2)


ans =

     2     4     6
     8    10    12
     2     4     6

Upvotes: 15

Related Questions