Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element wise multiplication of a matrix and a vector?

Tags:

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?

like image 762
6nagi9 Avatar asked Oct 22 '11 09:10

6nagi9


People also ask

What is the product of a matrix and a vector?

Matrix-vector product If we let Ax=b, then b is an m×1 column vector. In other words, the number of rows in A (which can be anything) determines the number of rows in the product b. The general formula for a matrix-vector product is Ax=[a11a12… a1na21a22…

What happens when you multiply a matrix by a vector?

We can only multiply an m × n matrix by a vector in Rn. That is, in Ax the matrix must have as many columns as the vector has entries. If we multiply an m × n matrix by a vector in Rn, the result is a vector in Rm. Ax = b.

What is an element-wise product?

In mathematics, the Hadamard product (also known as the element-wise product, entrywise product or Schur product) is a binary operation that takes two matrices of the same dimensions and produces another matrix of the same dimension as the operands, where each element i, j is the product of elements i, j of the ...


1 Answers

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 
like image 84
Adam Lear Avatar answered Sep 28 '22 01:09

Adam Lear