How do you compute the product of a matrix and each column of another matrix without a for loop in MATLAB, or is this just not possible?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
A 4x14 matrix (C) is multiplied by each column of a 14xN matrix (x) - hence by N individual 14 element column vectors - to form N individual 4 element column vectors - represented by a 4xN matrix (y) -, where N is a known yet large number, let's assume N = 132 for simplicity.
I use a for loop for this operation:
N = 132;
C = [0,1,0,0,0,0,0,0,0,-1,0,-0.68,0, 1.53;
0,0,0,1,0,0,0,0,0,-1,0,-0.64,0,-1.92;
0,0,0,0,0,1,0,0,0,-1,0, 0.86,0,-1.92;
0,0,0,0,0,0,0,1,0,-1,0, 0.91,0, 1.53];
x = rand(14,N);
for n = 1:N
y(:,n) = C*x(:,n);
end
y
This works fine, however, I wondered how this operation could be implemented without a for loop in MATLAB - all variables in the loop are available prior to the first iteration - to utilize MATLAB's matrix multiplication optimization and thus improve runtime performance, or is this just not possible?
Thanks in advance for any help!
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!