You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How I compute multiple sum in matlab for 4D matrix?
6 views (last 30 days)
Show older comments
How I compute multiple sum in matlab for 4D matrices as below? U and V are 4D matrices.

12 Comments
Amir Xz
on 11 Sep 2018
Why you don't use "for" loops?
U = rand(5,4,3,2);
V = rand(5,4,3,2);
I=5;J=4;M=2;N=3;
sumUV=0;
for i=1:I
for j=1:J
for k=1:I
for r=2:M
for s=2:M
for q=2:N
sumUV = sumUV + U(i,j,q,r)*V(k,j,q,s);
end
end
end
end
end
end
Matt J
on 11 Sep 2018
Edited: Matt J
on 11 Sep 2018
No, I mean you should do as I posted in my answer below. Reading the sum() commmand documentation would also probably be beneficial.
Answers (1)
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
6 Comments
Matt J
on 11 Sep 2018
In fact, it would probably be faster to do this as a vector-vector inner product,
A= partialU(:).'*partialV(:);
Mehdi
on 11 Sep 2018
believe me, your method is very different from add command in Maple.add is very simpler. What about if U and V be symbolic parameters?
Matt J
on 11 Sep 2018
Edited: Matt J
on 11 Sep 2018
What about if U and V be symbolic parameters?
But they are not. You said that they are numeric arrays, which is a smart choice, since you said that speed is important here. However, sum() should still work with symbolic variables. There is also symsum().
I don't see how add is simpler. Your expressions using add are a lot longer than the ones I've presented using sum.
Mehdi
on 11 Sep 2018
I am amazed. Here I ask my question again: What is the best way to calculate following expression in Matlab? Note U and V are known but q's are unknown (symbolic)
Mehdi
on 11 Sep 2018
I asked the question in new page as below: https://uk.mathworks.com/matlabcentral/answers/418526-what-is-the-best-way-to-calculate-multiple-summations-expression-in-matlab
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)