## 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?

A 4×14 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!A 4×14 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! A 4×14 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! matlab, matrix, for loop, matlab function, time series, speed MATLAB Answers — New Questions