0

So, I'm trying to simulate an arbitrary model of a bag of marbles (with replacement, if that makes a difference in how this works) and am running into issues displaying the results.

How I have it set up is the code asks for how many marbles are in the bag, the how many you would like to pick, and then how many different colors there are. (Defined as N, S, and k respectively). I then go through a loop between 1 and k in a cell array to name the colors of the marbles and then create a second array that simulates the probabilities by asking how many of each color there is in the bag. I then generate a random matrix that simulates 10 "games" (ie: rDist=randi(N,[10,S]); Now that I have the marbles that I've picked, I create another 10xS cell array and want to fill that cell array with the colors of the marbles based on the number picked. That is, let's say I have 10 marbles and 7 are red and 3 are green. If the PRNG picks 1:7, I want the results cell array to say "red" and if it chooses 8:10, I want "green" in the corresponding positions. I can do this for finite numbers, but I want to extend this to K marble colors with any number of distributions of marble colors. Can you offer any help?

My "finite" solution for 2 marble types is below:

for lc=1:10*S
    counter=0;
    if (rDist(lc)>=1 && (rDist(lc)<=Probabilities(1)))
        Results{lc}=Color{1};
    end
    counter=Probabilities(1);
    if (rDist(lc)>counter && (rDist(lc)<=counter+Probabilities(2)))
        Results{lc}=Color{2};
    end
end
Matt
  • 12,848
  • 2
  • 31
  • 53

1 Answers1

1

You can calculate the intervals that correspond to each color with cumsum. Then you need to find which interval each entry of rDist belongs to.

numPicks = 5;
numGames = 10;

names = {'red', 'white', 'blue'};
counts = [2 6 9];
N = sum(counts);

cumsumCounts = cumsum(counts);
rDist=randi(N, [numGames, numPicks]);

out = cell(size(rDist));
for i = length(counts):-1:1
  out(rDist <= cumsumCounts(i)) = names(i);
end

You could also do this with quantiz from the communication systems toolbox or randSample from the statistics and machine learning toolbox. Finally, you could use the more confusing one-liner out = names(arrayfun( @(x)( find(cumsumCounts >= x, 1) ), rDist));

Tokkot
  • 1,215
  • 7
  • 22