Matrix Multiplication & Splitting

27 ビュー (過去 30 日間)
Rahul Marwaha
Rahul Marwaha 2021 年 2 月 19 日
回答済み: James Tursa 2021 年 2 月 19 日
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 件のコメント
Rahul Marwaha
Rahul Marwaha 2021 年 2 月 19 日
Sorry to add clarification the A (3x3) should multiply C(3x3) to form a new 3x3 matrix. That matrix (3x3) can then be multiplied by B (3x1) to produce a new 3x1. Hope this clarifys the issue that a 3x3 should be found to then find the 3x1.
Steven Lord
Steven Lord 2021 年 2 月 19 日
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.

サインインしてコメントする。

採用された回答

Ive J
Ive J 2021 年 2 月 19 日
編集済み: Ive J 2021 年 2 月 19 日
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 件のコメント
Ive J
Ive J 2021 年 2 月 19 日
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}
Rahul Marwaha
Rahul Marwaha 2021 年 2 月 19 日
Thank you so much, I apologise it should have been as you've shown I totally forgot the order of multiplicaiton that would render the calculation impossible.

サインインしてコメントする。

その他の回答 (2 件)

James Tursa
James Tursa 2021 年 2 月 19 日
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);

James Tursa
James Tursa 2021 年 2 月 19 日
編集済み: James Tursa 2021 年 2 月 19 日
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 件のコメント
Rahul Marwaha
Rahul Marwaha 2021 年 2 月 19 日
Hi James, thanks for responding. It may not be clear but the 3xN matrix should be a 3x3 matrix. Essentially in my case C is a large 3xN matrix where N is any number divisible by 3 so that the matrix can be reshaped to form individual 3x3 matrices i.e C is 3x3 at minimum or 3x6 or 3x9 etc.
I'm then looking to have a 3x3 * 3x3 * 3x1 which would give a resultant 3x1 matrix. That is what i'm trying to resolve. Not sure if that helps but appreciate if you have any alternative suggestions, thanks.
James Tursa
James Tursa 2021 年 2 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by