Divide large size matrixes, multiply the small matrixes and sum the results together.

4 次查看(过去 30 天)
I divided two m x n matrixes A and B into several equal i x j matrices using mat2cell, respectively, and then I want to multiply the respective split matrixes and sum them together like
A11, … , A1j , ... Ai1, … , Aij are built from A, and
B11, … , B1j , ..., Bi1, … , Bij , are built from B,
What I want is
C = A11* B11 + … + Aij* Bij
Are there any commands in matlab can do this?
Many thanks!
  4 个评论
Andrei Bobrov
Andrei Bobrov 2019-5-18
What you want, Elements multiplication or matrix multiplication in any case your formula will give an error:
>> A = rand(32, 32 );
B = rand(32, 32 );
A11_77 = mat2cell (A, [ 2 2 2 2 2 2 4 2 2 2 2 2 2 4 ] , [ 2 2 2 2 2 2 4 2 2 2 2 2 2 4] );
B11_77 = mat2cell (B, [ 2 2 2 2 2 2 4 2 2 2 2 2 2 4 ] , [ 2 2 2 2 2 2 4 2 2 2 2 2 2 4] );
>> A11_77{6,7} * B11_77{6,7}
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows
in the second matrix. To perform elementwise multiplication, use '.*'.
>> A11_77{1,1} * B11_77{1,1} + A11_77{7,7} * B11_77{7,7}
Matrix dimensions must agree.
>>
Tony Cheng
Tony Cheng 2019-5-18
Dear Andrei ,
Thanks so much for your reminding!
Yeah, the dimension must match for the matrix multiplication. Just above I used an unrigorous example to illustrate the problem, but what I want is dividing the two big matrixes into many small ones, then multypling the small ones, and finally get the summation of each multiplication.
I think your solution
C = sum(A.*B,'all')
must be right. However, A and B are not divided into small one. And in my case, the dimension of A and B are very very high, if A*B are computed directly, it takes a very very long time to finish. Also, we want to observe the multiplication of small matrixes and get the physical meanings inside the mathematical expressions.
Accounting these two reasons, we make a strategy that, first, we divide the two big matrixes into small ones, and then multiply the small ones, finally add the multiplications together.

请先登录,再进行评论。

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2019-5-18
编辑:Andrei Bobrov 2019-5-18
C = sum(A.*B,'all')
for MATLAB < R2018b :
AB = A.*B;
C = sum(AB(:));

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by