MATLAB: How to multiply a vector with each column of a matrix

multiplication

Say I have a column matrix [1;2;3;4].
I have another matrix of order 3*4. Say the matrix is [1 4 7 10; 2 5 8 11; 3 6 9 12].
I want to get the result as [1 8 21 40; 2 10 24 44; 3 12 27 48].
So the each column of the 3*4 matrix should be multiplied by each elemnt of the column matrix one by one.

Best Answer

A = [1 4 7 10; 2 5 8 11; 3 6 9 12];
b = [1;2;3;4];
C = bsxfun(@times,A,b.')