MATLAB直方图显示额外的值

时间:2012-06-04 02:02:18

标签: matlab probability histogram

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

由于轧制7的总和的概率是6/36 = 1/6,因此通常在最初的几个轧辊之一上发生。这里我有一个行向量“rollbins”,其中第i个条目保持实验的频率需要“i”卷。在经过多次迭代后,rollbins的前几个元素变大,每个后续条目变小,直到第45个通常为零。

我已经将hist()函数与bins矢量参数和xlim()问题一起使用我使用xlim()来限制x轴上的显示仅为0-45 。但是,输出不受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]) )中移除可以实现这些效果。

{{1}}

然而我现在意识到这些酒吧太短了,而且这些垃圾箱按照我的预期以.1而不是1的间隔出现。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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])

screenshot

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

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);

答案 2 :(得分: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])