how should I cal. the sum of all the combinations?
Show older comments
Dear All, For one vector A=(a1, a2, ...a3000). I want to calculate the sum(ai * aj^2) where i,j are different indices, i not equal to j. If I use the co_vol function it costs too much time to run the code. So I am thinking, is there a formula exisiting to calculate this sum? Like the case I showed under the "%%%%%%%%" line below, for the sum(ai * aj* ak) case. (where i j k are different from each other)
Thanks a lot in advance
function co_vol=co_vol(in)
in=in; N=length(in);
% aa=zeros(N^2,1); aae=[];
parfor i=1:N for j=1:N if j~=i ae=in(i)*in(j)^2; else ae=0; end
aae =[aae ae];
end
end
co_vol=sum(aae); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 I am calculating the sum(ai * aj * ak) where i,j,k are different indices. based on this formula: (sum(A)^3-3*sum(A.^2)*sum(A)+2*sum(A.^3))
instead of the code below. This saves me a lot of computation time. function co_cov=co_cov(in)
in=in; N=length(in);
aae=[];
parfor i=1:N
for j=1:N
for k=1:N
if (i~=j)&&(i~=k)&&(j~=k)
ae=in(i)*in(j)*in(k);
else
ae=0;
end
aae=[aae ae];
end
end
end
co_cov=sum(aae ); end
Answers (0)
Categories
Find more on Structural Mechanics 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!