Element wise multiplication of a matrix and a vector?

6nagi9 picture 6nagi9 · Oct 22, 2011 · Viewed 19.5k times · Source

Is there an in-built function in octave to multiply each column of a m X n element-wise with a column vector of size m that is more efficient than using a loop?

Answer

Adam Lear picture Adam Lear · Oct 22, 2011

You can replicate the vector as many times as you need to turn it into a m x n matrix as well and then use the built-in element-wise multiplication operator .*:

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

>> A .* repmat(B, 1, columns(A))
ans = 

    1    2
    6    8
   15   18