Matrix-vector multiplication vectorization

127 vues (au cours des 30 derniers jours)
Shahriar
Shahriar le 30 Août 2016
Commenté : Ivan Dubrovskiy le 2 Juil 2022
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)

Réponse acceptée

Guillaume
Guillaume le 30 Août 2016
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

Plus de réponses (1)

John D'Errico
John D'Errico le 30 Août 2016
Modifié(e) : John D'Errico le 30 Août 2016
If you are storing tham in a 5x3 array, then they are 1x3 vectors, not 3x1. There is a difference.
Anyway,
A*v'
Won't work? Did you try it? If you insist on having the result be transposed again, then you could do it as:
(A*v')'
or you might recognize that this is the same as
v*A'
As long as all arrays are real. If they are complex, then becareful, as ' and .' are different operators, and you need to know which is correct.
  1 commentaire
Shahriar
Shahriar le 30 Août 2016
That works. Thanks :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by