MATLAB: Matrix Multiplication & Splitting

matrix manipulation

Hi I'm trying to extract given values from a large matrix and mulitply it by other known matrices e.g.
A = [1 2 3: 4 5 6: 7 8 9] % 3x3 Matrix which is always the same
B = [1 2 3] % 3x1 Matrix which also stays the same
C = % A matrix of 3x12 or 3x15 etc, e.g. The matrix could be split into indiviudal 3x3 matrices
As shown above I compute the matrix C which is formed from varying numbers of 3×3 matrices. I'm then attempting to call each 3×3 from the matrix and multiply it by the A and B e.g.
D1 = A * B * C(:,1:3)
D2 = A * B * C(:,4:6)
D3 = A * B * C(:,7:9) % etc until I have done it for all the sub 3x3 matrices
As shown the large matrix C is split into equal 3×3 matrices and then is multiplied by A and B. I was wondering how this could be done for C being any 3 by matrix e.g. 3×12 or 3×36 i.e. it can be evenly split into individual 3×3 matrices.
Any help is greatly appreciated, thanks!

Best Answer

Given your input criteria, you can simply reshape C:
sz = size(C, 2);
C = reshape(C, 3, 3, sz/3);
D = arrayfun(@(i)A*B*C(:,:,i), 1:sz/3, 'UniformOutput', false);