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?
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Simon
il 4 Lug 2024 alle 12:41
Commentato: Simon
il 4 Lug 2024 alle 14:06
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 Commenti
Risposta accettata
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!