f4r3in
님이 질문을 제출함. 31 Oct 2019

hi, I want to write a script in MATLAB that gives me sum of all possible combinations of a few numbers.

for example I have these numbers as inputs :

120 , 250 , 450

and I want to have sum of all combinations of these numbers means that I want these outputs :

120 , 250 , 370 , 450 , 570 , 700 , 820

I can solve this problem by "ff2n" command if numbers are low (like this example 3 numbers) like this :

ff2n(3) %due to 3 numbers (120,250,450)

>> 0 0 0

0 0 1

0 1 0

1 0 0

0 1 1

1 0 1

1 1 0

1 1 1

then I can sum all combinations of numbers by "if" and "for" commands.

but if the number of numbers become more like 135 numbers what should I do ???!!!

John D'Errico
님의 답변 31 Oct 2019

John D'Errico
님이 편집함. 31 Oct 2019

It is often true that people decide to do something unimaginably huge, because something small seemed easy enough. Computers are fast and big, so they can do anything, right?

The problem is you don't have enough memory to solve this using brute force. You don't have enough time to solve it in your lifetime.

All possible combinations of those numbers means you need to generate all subsets of numbers, then summing the subsets. A simple trick to generate all such combinations is just to use dec2bin.

V = [120 , 250 , 450];

n = length(V);

allsums = (dec2bin(1:2^n-1) - '0')*V(:)

allsums =

450

250

700

120

570

370

820

(Think about why that works. It does.)

Of course, many of those sums will be replicated in the above, because there may be multiple ways to generate a specific sum. So, you could use unique on that result. In fact, this is a serious problem, because computing all of the possible sums is a really bad idea when n is large. As has been pointed out, when there are 136 elements in the vector, there are then 2^136-1 possible combinations where at least one of the numbers is included in the sum. That number is hugely more than you can work with, even though the possible sums are far more finite.

So you need to work more intelligently. Brute force is NEVER a good choice, if an alternative is available.

The question is if you really want to find the unique possible combinations. For example, suppose you decided to find all possible sums of the numbers 1:136?

V = 1:136;

sum(V)

ans =

9316

There are only 9316 possible sums to consider. Once we find one way to create the sum 1136 (for example), we need not look further to see if that sum is possible. In fact, here is a trivial way to find a partition of the number 1136, in terms of the elements 1:136.

136 + 135 + 134 + 133 + 132 + 131 + 130 + 129 + 76

ans =

1136

Surely you can see how I found that sum.

The point of all this? Work smart, and your computer won't grind to a halt on everything you do.

Walter Roberson
31 Oct 2019

240 (120+120) has 0.01*0.01 (10^-4) probability.

10^-4 probability of failure? So a single 240 MW station is 100 times more reliable than a pair of 120 MW stations? I don't think so.

f4r3in
31 Oct 2019

Walter Roberson
31 Oct 2019

For example : 120 has 0.01 probability And 240 (120+120) has 0.01*0.01 (10^-4) probability.

That is bad statistics. 10^-4 might be the probability that both would fail, but it is not the probability that either would fail. The probability that either would fail is according to

>> 1-(1-0.01)*(1-0.01)

ans =

0.0199

If you have 69 items all of failure probability 0.01, then

>> 1-(1-0.01)^69

ans =

0.500162970100801

Over 50% probability that at least one would fail.

If you are trying to find combinations that lead to the lowest probability of failure in providing a particular output, then you could do that with some work, but I would wonder if that is what is what would be desired in an electrical generation system? Suppose I put together a combination that has a 0.01 probability of failure for a given output, but when one of the stations does fail, it takes (say) half of the capacity down with it; and suppose I put together a different combination that has a 0.0101 probability of failure for a given output, but 93% of the time when it does fail, it loses only 1/10th of the output. Would it not be the weighted "resiliance" that would be desired, rather than simply the lowest probability in absolute terms?

로그인 to comment.

Opportunities for recent engineering grads.

Apply Today
## 댓글 수: 6

## James Tursa (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_761933

## f4r3in (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_761939

## Walter Roberson (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_761947

## f4r3in (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_762035

Thank you for answering, but I want to write a general script for COPT table that support any inputs. And input form is something like my example in above. Also I want to have all unique sums (combinations) of capacities. For example 20*120 have 20 unique sums like: 120, 240, 360,... And 30*160 have 30 unique sums but the unique sums of each capacities are not unique and we have to unique it. For example : Sum of four 120 is 480 and also sum of three 160 is 480 too. So I dont need two 480 because one of them shows the unique capacity I want.

## Walter Roberson (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_762043

## f4r3in (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/488424-all-combinations-of-numbers#comment_762066

로그인 to comment.