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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!