找到你要的答案

Q:Simulation of an arbitrary bag of marbles

Q:任意包弹子的模拟

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

所以,我试图模拟一个任意模型的一包大理石(更换,如果这使得这如何工作的差异),并运行到显示结果的问题。

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?

我的“有限”2大理石类型的解决方案如下:

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
answer1: 回答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));

你可以计算间隔对应每个颜色的Cumsum。然后,你需要找到它的每一项都属于区间rdist。

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

你也可以做这个量化从通信系统工具箱或randsample从机器学习和统计工具箱。最后,你可以使用更多的混乱的一套=名称(arrayfun(@(x)(找到(cumsumcounts >;= x,1)),rdist));

matlab  statistics  probability  n-dimensional