Matrix Multiplication & Splitting
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rahul Marwaha
el 19 de Feb. de 2021
Respondida: James Tursa
el 19 de Feb. de 2021
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 3x3 matrices. I'm then attempting to call each 3x3 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 3x3 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. 3x12 or 3x36 i.e. it can be evenly split into individual 3x3 matrices.
Any help is greatly appreciated, thanks!
4 comentarios
Steven Lord
el 19 de Feb. de 2021
If you adopt the approach suggested by the other posters and turn your C matrix into a 3-dimensional array D, in release R2020b and later you could use the pagemtimes function to compute the product of your A matrix with each page of D.
Respuesta aceptada
Ive J
el 19 de Feb. de 2021
Editada: Ive J
el 19 de Feb. de 2021
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);
3 comentarios
Ive J
el 19 de Feb. de 2021
I guess you meant B*A*C, otherwise it's impossible!
A = [1 2 3; 4 5 6; 7 8 9];
B = [1 2 3];
C = reshape(1:36, 3, 12);
sz = size(C, 2);
C = reshape(C, 3, 3, sz/3);
D = arrayfun(@(i)B*A*C(:,:,i), 1:sz/3, 'UniformOutput', false)
1×4 cell array
{1×3 double} {1×3 double} {1×3 double} {1×3 double}
Más respuestas (2)
James Tursa
el 19 de Feb. de 2021
Based on your latest posts, it sounds like you really want A*C(3x3 slice)*B. So again it would be nice to do all the multiplies in one fell swoop and then pick off your desired results. E.g.,
D = B.' * reshape(permute(reshape(A*C,3,3,[]),[2 1 3]),3,[]);
D = reshape(D,3,[]);
and the results are in D(:,1), D(:,2), etc.
Or just use the following as Steven Lord suggests:
D = pagemtimes(reshape(A*C,3,3,[]),B);
0 comentarios
James Tursa
el 19 de Feb. de 2021
Editada: James Tursa
el 19 de Feb. de 2021
Your dimensions don't work. A*B is going to be 3x1. You can't multiply this by a 3xN matrix.
That being said, suppose you did have dimensions that did work for A*B. Then you don't need to split anything up for the multiply. You can just do A*B*C directly and then pick off the columns of the result that you want. E.g., suppose we have the simpler problem below
A is 3x3
C is 3x12
and we want A multiplied by individual 3x3 parts of C. Then instead of doing these individual matrix multipies
D1 = A*C(:,1:3)
D2 = A*C(:,4:6)
D3 = A*C(:,7:9)
D4 = A*C(:,10:12)
You can just do this all in one fell swoop
D = A*C
and then reshape it for convenience
D = reshape(D,3,3,[])
Then you can pick off D(:,;1), D(:,:,2), etc. for your results.
2 comentarios
James Tursa
el 19 de Feb. de 2021
If B is really 1x3 and the order is supposed to be B*A*C(:,slice), then it is just
D = B*A*C;
Then you can reshape
D = reshape(D,1,3,[]);
and you can pick off D(:,:,1), D(:,:,2), etc. for the results.
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!