MATLAB: Matrix-vector multiplication vectorization

matrixvectorization

Dear All, I have a simple 3*3 matrix(A) and large number of 3*1 vectors(v) that I want to find A*v multiplication for all of the v vectors. Instead of using "for" loop which takes so much time, how can I vectorize the matrix multiplication? Example: A=[1 2 3;4 5 6;7 8 9]; v=[1 2 3; 4 5 6; 7 8 9;10 11 12;13 14 15] (5 set of 3*1 vectors)

Best Answer

Note that v=[1 2 3; 4 5 6; 7 8 9;10 11 12;13 14 15] is 5 rows of 1x3 vectors, not 3x1 vectors.
Simply multiply your matrix by the vector matrix to get all the result vectors at once:
A=[1 2 3;4 5 6;7 8 9];
v=[1 2 3; 4 5 6; 7 8 9;10 11 12;13 14 15];
v = v.'; %transpose so that v is indeed 5 columns of 3x1 vectors
B = A*v %each column of B is A*each column of v by definition of matrix multiplication