vectormatrixoctave

Element wise multiplication of a matrix and a vector?


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?


Solution

  • 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