在 MatLab 中从直方图中获取最大和最小图形值

Obtaining the Maximum and Minimum Graphed Values from a Histogram in MatLab

我被赋予了在 MatLab 中创建一个程序的任务:

  1. 模拟滚动两个 6 面骰子 N 次的实验,将滚动的 2 个值相加,然后绘制获得这些值的频率。
  2. 然后能够打印最频繁和最不频繁的滚动结果之间的百分比差异。

我已经想出了如何做第一部分:

numberOfDice = 2; %Number of dice to be rolled
numberOfDots = 6; %Number of max. dots allowed on a die face
numberOfRolls = 100000; %Number of times the die(s) are rolled

%#Generate the rolls and obtain the sum of the rolls
AllRoll = randi(numberOfDots, numberOfRolls, numberOfDice);
sumOfRoll = sum(AllRoll, 2);

%#Determine the bins for the histogram
Bins = (numberOfDice:numberOfDots * numberOfDice)';

%#Build the histogram
hist(sumOfRoll, Bins);
title(sprintf('The Frequency of Different Sums from %d %d-sided Dice after %d Rolls', numberOfDice, numberOfDots, numberOfRolls));
xlabel(sprintf('The Sum of %d Dice', numberOfDice));
ylabel('Count');

我对如何实现第二部分感到困惑,因为我不确定如何从直方图中获取最大值和最小值。这甚至有可能吗,还是我必须以另一种方式去做?我很迷路。任何帮助都会很棒。

您只需修改现有代码即可将直方图值分配给变量,并使用它来查找百分比差异。

histValues = hist(sumOfRoll, Bins);

此处,histValues 保存每个 bin 的直方图值。然后,您可以使用这些值来计算差异和百分比差异。

diffInOutcomes = (max(histValues) - min(histValues))
percentDiff = (diffInOutcomes)*100/numberOfRolls