all combinations of numbers

5 views (last 30 days)
f4r3in
f4r3in on 31 Oct 2019
Commented: SAMITA on 17 Nov 2023
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 ???!!!
  7 Comments
f4r3in
f4r3in on 31 Oct 2019
I think your way is more complex than my way. As I comment as a answer of john's comment : we can use "nchoosek" command to get all possible subsets of inputs and then we can get the sum of each subset and by using unique command we can get all possible sums of any inputs. But I have other problem named probabilities. I describe it in the comment I put to john's answer. Would you read it?!
SAMITA
SAMITA on 17 Nov 2023
have you got the code for this. I am also stucked with a problem having 32 generators

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 31 Oct 2019
Edited: John D'Errico on 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.
  4 Comments
f4r3in
f4r3in on 31 Oct 2019
No, it is outage probability. It means that probability of outage of 120 MW capacity (0.01) is more than the probability of outage of 240 (120+120) MW (10^-4).
Walter Roberson
Walter Roberson on 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?

Sign in to comment.

Categories

Find more on Programming 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!