# How I compute multiple sum in matlab for 4D matrix?

2 views (last 30 days)
Mehdi on 11 Sep 2018
Commented: Mehdi on 11 Sep 2018
How I compute multiple sum in matlab for 4D matrices as below? U and V are 4D matrices.
Matt J on 11 Sep 2018
Edited: Matt J on 11 Sep 2018
If they are "similar", my method - which is to use the sum command - is applicable. Have you read the documentation on sum()? It is the best analogue of add that you will find in Matlab.

Matt J on 11 Sep 2018
Edited: Matt J on 11 Sep 2018
Notice that the only indices shared between U and V are j and q. Therefore, you can minimize multiplication operations as follows,
partialU=sum(sum(U,4),1); %sum over i and r
partialV=sum(sum(V,4),1); %sum over k and s
A=sum(partialU(:).*partialV(:)); %sum over j and q
Mehdi on 11 Sep 2018

### Categories

Find more on Logical in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!