Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 913
erogol Avatar asked Nov 10 '12 20:11

erogol


2 Answers

 bsxfun(@times,m1,m2)


ans =

     2     4     6
     8    10    12
     2     4     6
like image 82
Nasser Avatar answered Sep 28 '22 00:09

Nasser


You could also use

 result = diag(m2)*m1;
like image 31
bazz Avatar answered Sep 28 '22 00:09

bazz