MATLAB Answers

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

Tony Cheng

### Tony Cheng (view profile)

さんによって質問されました 2019 年 5 月 18 日

### Tony Cheng (view profile)

さんによって コメントされました 2019 年 5 月 18 日
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!

Tony Cheng

### Tony Cheng (view profile)

2019 年 5 月 18 日
An example is:
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] )
C = A11_77{1,1} * B11_77{1,1} + A11_77{1,2} * B11_77{1,2} + ...
A11_77{1,7} * B11_77{1,7} +
...
A11_77{7,1} * B11_77{7,1} + A11_77{7,2} * B11_77{7,2} + ...
A11_77{7,7} * B11_77{7,7} + ;
This is just an example.
However, in my project , the size of A and B are 500 * 500 , and i want to split them into 120 small matrixes and sum their respective multipications together, as shown in C in the example.
So, are there some methods to do it , without formatting the long process in generating C as in the example?
Many thanks!
Cheers
Andrei Bobrov

### Andrei Bobrov (view profile)

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 (view profile)

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.

サインイン to comment.

R2018b

## 1 件の回答

2019 年 5 月 18 日

### Andrei Bobrov (view profile)

2019 年 5 月 18 日

C = sum(A.*B,'all')
for MATLAB < R2018b :
AB = A.*B;
C = sum(AB(:));

#### 0 件のコメント

サインイン to comment.

Translated by