Nested for-loops too slow
Show older comments
Hello,
I'm having trouble to compute a summation with four indices. My code below is very time consuming. The problem is have to compute for every i,j,k,l the three smallest elements of A. Any other suggestions that run a bit faster than my code? (N is approximately 156)
sum = 0;
for i=1:N
for j=1:N
for k=1:N
for l=1:N
A = [N-(i-1),N-(j-1),N-(k-1),N-(l-1)];
B = sort(A);
sum = sum + B(1)*B(2)*B(3);
end
end
end
end
Thank you very much!
Accepted Answer
More Answers (2)
Teja Muppirala
on 18 Apr 2011
You really shouldn't use "sum" as a variable name. That is the name of a very useful built-in function. I'll call your variable "S" instead.
Looking at the code, you can deduce that S should be a polynomial function of N. Doing a little bit of curve fitting you can indeed get an analytic expression: S(N) =
(1/420)*(30*N.^7 + 105*N.^6 + 147*N.^5 + 105*N.^4 + 35*N.^3 - 2*N)
So If were to type into MATLAB:
S = @(N)(1/420)*(30*N.^7 + 105*N.^6 + 147*N.^5 + 105*N.^4 + 35*N.^3 - 2*N);
S(uint64(156))
Then you have your very long answer:
S = 164235165014258
3 Comments
Egon Geerardyn
on 18 Apr 2011
That might work around N = 156, but as you are curve fitting, you are making an error.
It might be possible to get a closed form expression, but yours certainly isn't valid for any N. Just compare the output of the reference code and your formula for small values of N; there is already a large discrepancy which means that all you will get are approximated values and you won't even know how well the real value is approximated.
I am pretty confident it should be possible to derive a more closed-form formula, but curve fitting is not the way to go.
Teja Muppirala
on 18 Apr 2011
I believe the polynomial given is correct for all N, not just around 156. The code below confirms this for N = 1 to 20. I'm not sure what discrepancies you are referring to. There are 4 for loops, and in the inner most loop you are adding factors of order N^3, so it's not quite unexpected that the final result would be a polynomial of order 7. What I meant by "curve fitting", is an analytic curve fitting of a 7th order polynomial at 7 points.
clear S;
for N = 1:20
S(N,1) = 0;
for i=1:N
for j=1:N
for k=1:N
for l=1:N
A = [N-(i-1),N-(j-1),N-(k-1),N-(l-1)];
B = sort(A);
S(N) = S(N) + B(1)*B(2)*B(3);
end
end
end
end
end
S_ANALYTIC = @(N)(1/420)*(30*N.^7 + 105*N.^6 + 147*N.^5 + 105*N.^4 + 35*N.^3 - 2*N);
S2 = S_ANALYTIC(uint64(1:20)')
[S S2]
aretheyequal = isequal(S2,S)
Teja Muppirala
on 18 Apr 2011
That should be:
A 7th order polynomial fit to "8 points" not "7 points"
Robert Cumming
on 18 Apr 2011
Pulling out the i, j and k elements only when they changewill speed it up:
sum = 0;
A = zeros(4,1);
for i=1:N
A(1) = N-(i-1);
for j=1:N
A(2) = N-(j-1);
for k=1:N
A(3) = N-(k-1);
for l=1:N
A(4) = N-(l-1);
B = sort(A);
sum = sum + B(1)*B(2)*B(3);
end
end
end
end
Categories
Find more on Linear Least Squares 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!