File Exchange

image thumbnail

All Sums

version 1.5 (2.27 KB) by

Distribution of unique sums among all combinations of vectors.

0 Downloads

Updated

View License

ALLSUMS Distribution of unique sums among all combinations of vectors.

[S,C]=ALLSUMS(IN) depending on input IN, will do one of two different procedures for calculating all unique combinations of sums.
 
The output vectors, S and C, are respectively the list of unique sums, and the count distribution for those sums among all combinations.
 
 
SINGLE INPUT:
 
[S,C]=ALLSUMS(VEC) for a single input vector of length N, returns the distribution of unique sums among the (2^N)-1 combinations of non-empty subsets of elements within VEC.
 
EXAMPLE:
Number of ways to have a total of 250 using only the odd numbers from 1 to 100.
[S,C]=allsums(1:2:100);
num=C(S==250)
 
 
MULTIPLE INPUTS:
 
[S,C]=ALLSUMS(VEC,K) with vector VEC of length N, assumes theequivalent scenario of rolling a N-sided die, with values VEC, K times and returns the distributions of unique sums among all N^K combinations of values.
 
[S,C]=ALLSUMS(VEC1,K1,VEC2,K2,...) with vectors of length N1, N2, ... assumes the equivalent scenario of rolling a N1-sided die, with values VEC1, K1 times; then rolling a N2-sided dice, with values VEC2, K2 times; etc. and returns the distribution of unique sums among all (N1^K1)*(N2^K2)*... combinations of values
 
SINGLE VECTOR, REPEATED
Probability distribution of rolling a regular 6-sided die 100 times
[S,C]=allsums(1:6,100);
plot(S,C./sum(C)), grid on
 
MULTIPLE VECTORS, REPEATED
Probability of the total yielding exactly zero when rolling a
A) six-sided die with values [-5:0] ten times, and then a
B) ten-sided die with values [-4:5] twenty times.
[S,C]=allsums([-5:0],10,[-4:5],20);
prob=C(S==0)/sum(C)

Comments and Ratings (2)

Mike Sheppard

Mike Sheppard (view profile)

For unequal probabilities see "Sum of probability distributions" #34082

Updates

1.5

Updated help section

1.4

Rephrased examples and summary

1.3

Generalized to multiple inputs, fixed error checking and modified code for performance.

1.2

Generalized for multiple inputs, and across different vectors.

MATLAB Release
MATLAB 7.12 (R2011a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video