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

How I compute multiple sum in matlab for 4D matrices as below? U and V are 4D matrices.

12 Comments

Use sum.
Yes as you see in enclosed photo there are 6 sum.
Why do some summations start at 0 and others at 1?
it is the format my problem, I cant change it.
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
Mehdi
Mehdi on 11 Sep 2018
Edited: Mehdi on 11 Sep 2018
becasuse the speed is very low, I have many similar expressions must be computed in program.
Mehdi's comment relocated here:
in Maple you can simply do it by add command like below:
A=add(add(add(add(add(add(U[i,j,q,r]*V[k,j,q,s],i=0..I),j=0..J),k=0..I),r=1..M))s=1..M),q=1..N)
is there any similar way to do it in Matlab?
Matt J
Matt J on 11 Sep 2018
Edited: Matt J on 11 Sep 2018
If you mean, how can you sum over specific dimensions of an array, my answer shows how to do that using the sum() command.
you mean I must replace add with sum like:
A=sum(sum(sum(sum(sum(sum(U[i,j,q,r]*V[k,j,q,s],i=0..I),j=0..J),k=0..I),r=1..M)),s=1..M),q=1..N)
Matt J
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.
Mehdi
Mehdi on 11 Sep 2018
Edited: Mehdi on 11 Sep 2018
As I said I have many similar expressions like that and it seems your method is not applicable for more expressions. I am searching a similar command like add in Maple.
Matt J
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.

Sign in to comment.

Answers (1)

Matt J
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

In fact, it would probably be faster to do this as a vector-vector inner product,
A= partialU(:).'*partialV(:);
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
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.
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)
Matt J
Matt J on 11 Sep 2018
Edited: Matt J on 11 Sep 2018
You should edit your original post with this new equation and information about q_r and q_s. The new expression is rather unclear. You have the symbol q being used for two different things, both as a symbolic variable and as an index of summation.
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

Sign in to comment.

Categories

Products

Release

R2018a

Asked:

on 11 Sep 2018

Commented:

on 11 Sep 2018

Community Treasure Hunt

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

Start Hunting!