Take all possible combinations for more than 15 elements
3 views (last 30 days)
Show older comments
Hi all,
So I have an array A with 65 elements. I want to take sum of all possible 3 elements combination i.e.,
- Item one A1+A2+A3, A1+A2+A4, A1+A2+A5,...A1+A2+A65;
- Item one A1+A3+A4, A1+A3+A5, A1+A3+A6,...A1+A3+A65;and so on.How to do this?
0 Comments
Answers (3)
John D'Errico
on 11 Aug 2017
In most cases of this sort, the answer is: You need to spend the money to buy a big enough computer to solve the problem.
On this small problem, the answer is simple. Use nchoosek.
sum(A(nchoosek(1:65,3)),2)
Of course, then you will almost always come back and say that works for small problems, but what you really wanted to solve is how to do this when the vector has 10000 elements, and you want to take all possible sums of 5 elements, or 50. Then the answer will be: see my first comment. :)
4 Comments
John D'Errico
on 13 Aug 2017
Not storing the computations is irrelevant. You need to do the computations.
Re-read what I said.
"Of course, then you will almost always come back and say that works for small problems, but what you really wanted to solve is how to do this when..."
As I showed, it is trivial to do for combinations of 3 elements at a time. There are 43680 such combinations.
nchoosek(65,3)
ans =
43680
However,
nchoosek(65,32)
Warning: Result may not be exact. Coefficient is greater than 9.007199e+15 and is only accurate to 15 digits
> In nchoosek (line 92)
ans =
3.6097e+18
Can you generate anything like 10^18 combinations of elements? It does not matter how you will do the sums. You need to make something on the order of 3.6e18 combinations. Oh, then if you will add them up, there will be 31 adds to do each sum. So roughly 1e20 floating point operations. 100 quintillion operations or so. Do you have a really fast computer? I hope so.
I figure, if your computer can do a billion adds per second, then the additions alone would take roughly 3.6 years to finish. Hey that is not too bad! Pick up a copy of something exciting like the Oxford English Dictionary, and read it through a few dozen times while you wait.
For some reason, people never seem to understand the limits of their computers. They see too many tv shows and movies, with computers that seem all powerful, capable of doing anything in a split second. In fact, it is trivial to define a problem that will take the lifetime of the universe (thus until it dies of heat death) to solve on a computer the size of the entire universe.
You need to decide if you REALLY need to do what you want. Then find a good way of avoiding exactly what you want to do.
alice
on 11 Aug 2017
You can look at the nchoosek function:
doc nchoosek
Then you should be able to write something like this:
A = rand(65,1);
result = sum(nchoosek(A,3),1);
3 Comments
Shipra Jain
on 11 Aug 2017
Sorry. I am referring to elements Alice. My element length is varying from 2-64. I have written only three as example. This function doesn't work beyond 15. What do I do. Please suggest.
David Goodmanson
on 13 Aug 2017
Edited: David Goodmanson
on 13 Aug 2017
Hello shipra,
If I understand this correctly, you want to find all possible combinations of the 65 elements of A taken 3 at a time, then sum each of those triples up and later take the average of all the sums. And then generalize that from 3 at a time to k at a time.
If A has n elements, then there are C(n,k) = nchoosek(n,k) = n!/((n-k)!k!) possibilities. Each of those brings in k elements of A, so the total number of occurrences of elements of A is C(n,k)*k. But by symmetry, each element of A is occurs the same number of times, so each element occurs C(n,k)*k/n times. The total of all the sums is C(n,k)*(k/n)*sum(A), and the mean over all C(n,k) possibilities is simply (k/n)*sum(A):
n = 65;
A = rand(1,n);
k = 4;
setofsums = sum(A(nchoosek(1:n,k)),2);
meansums = mean(setofsums)
sAkn = sum(A)*k/n
meanofsums = 1.7045
sAkn = 1.7045
k = 64;
setofsums = sum(A(nchoosek(1:n,k)),2);
meansums = mean(setofsums)
sAkn = sum(A)*k/n
meanofsums = 29.9039
sAkn = 29.9039
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!