0

我需要在掷两个骰子时产生 7 的总和之前生成掷骰数的概率直方图。实验正常运行,通过 10,000 次迭代,我得到的数据看起来与您预期的一样。然而,我在直方图中显示这些数据时遇到了很多麻烦。问题是有大量额外数据似乎打印到直方图上,而这些数据在我传递给的向量中不存在hist()。这显示为 x 轴上较大值的大量无限大的 bin。

由于掷出 7 和的概率是 6/36 = 1/6,因此这通常发生在前几次掷骰中的一个上。这里我有一个行向量“rollbins”,其中第 i 个条目保存需要“i”个滚动的实验的频率。经过多次实验迭代后,rollbins 的前几个元素很大,随后的每个元素都变小,直到第 45 个元素通常为零。

我已经将该hist()函数与 bins 向量参数一起使用,并且根据这个问题,我曾经xlim()将 x 轴上的显示限制为仅 0-45。然而,输出不受有无限制xlim()

任何帮助是极大的赞赏 :)

iters = 1000;
% do not consider extreme results
maxrolls = 45;
% rollbins(i) is how many experiments occured with i rolls
rollbins = zeros(1, maxrolls);

for r=1 : 1 : iters
    % roll die until get sum of 7, note times taken
    sum = 0;
    % the amount of rolls the experiment takes
    rolls = 0;
    while sum ~= 7
        rolls = rolls + 1;
        % sum two rolls of a die (same as one roll two dies)
        sum = floor( 6*rand(1) + 1 ) + floor( 6*rand(1) + 1 );
    end

    % assign if within the vector's limits; discards outliers
    if rolls < maxrolls
        rollbins(rolls) = rollbins(rolls) + 1;
    end
end

% 1,2,3...45
range = 1:1:maxrolls;
% limit the values on x-axis to 0-45
xlim([0 maxrolls]);
% the histogram shows more than 45 vertical bars
hist(rollbins, range)

编辑:xlim()调用应该在hist()函数之后。离开最后一个图形函数 ( ) 的分号ylim可以实现这些效果。

hist(rollbins, range);
xlim([0 maxrolls-1]);
ylim([0 iters / 5])

但是,我现在意识到这些条形仍然太短,并且这些条形图的间隔显示为 0.1 而不是 1,正如我所预期的那样。

4

3 回答 3

1

您正在记录滚动计数的频率,但您应该只记录滚动计数本身,然后让 hist 在直方图中显示频率。

此外,您需要在生成直方图后(而不是之前)应用 xlim。

rollbins = zeros(1, maxrolls);
numberofrolls = [];   % Initialise numberofrolls

if rolls < maxrolls
    rollbins(rolls) = rollbins(rolls) + 1;
    numberofrolls (end+1) = rolls;  % Record # of rolls
end

hist(numberofrolls);    % Generate histogram
于 2012-06-04T02:55:18.043 回答
0

以下是我将如何实现此模拟:

iters = 1000;               %# number of times to run simulation
maxrolls = 45;              %# max number of rolls to consider
numRolls = nan(iters,1);    %# store number of rolls in each run
for r=1:iters
    %# rolls dice "maxrolls"-times, and compute the sums
    diceSums = sum(randi([1 6],[maxrolls 2]), 2);

    %# find the first occurence of a sum of 7
    ind = find(diceSums==7, 1, 'first');

    %# record it if found (otherwise noted as NaN)
    if ~isempty(ind)
        numRolls(r) = ind;
    end
end

%# compute frequency of number of rolls, and show histogram
counts = histc(numRolls, 1:maxrolls);
bar(1:maxrolls, counts, 'histc'), grid on
xlabel('Number of dice rolls to get a sum of 7')
ylabel('Frequency')
xlim([1 maxrolls])

截屏

如果你觉得有点冒险,这里是大循环的完全矢量化版本:

numRolls = cellfun(@(v) find(v,1,'first'), ...
    num2cell(sum(randi([1 6],[iters maxrolls 2]),3) == 7, 2), ...
    'UniformOutput',false);
numRolls(cellfun(@isempty,numRolls)) = {NaN};
numRolls = cell2mat(numRolls);
于 2012-06-04T16:17:08.367 回答
0

这是我最终得到的解决方案(我还不太熟悉矢量化)

iters = 10000;
% preallocation of experiments row vector, one element for every experiment
experiments = zeros(1,iters);
for i=1 : 1 : iters
    % roll die until get sum of 7, note times taken
    sum = 0;
    rolls = 0;
    while sum ~= 7
        rolls = rolls + 1;
        sum = floor(6*rand(1)+1) + floor(6*rand(1)+1);
    end

    % save the number of rolls this experiment took
    experiments(i) = rolls;
end

% do not plot experiments that took more than 50 rolls
bins = 0:1:50;
hist(experiments, bins);
xlim([0 50]);
ylim([0 1750])
于 2012-06-05T21:50:08.810 回答