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

时间:2016-09-19 03:29:57

标签: matlab max histogram minimum

我在MatLab中获得了创建程序的任务:

  1. 模拟滚动两个6面模具N次的实验,将所滚动的2个值相加,然后绘制获得这些值的频率。
  2. 然后能够打印最常见和最不频繁的滚动结果之间的百分比差异。
  3. 我已经想出如何做第一部分:

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

    enter image description here

    我对如何实现第二部分感到磕磕绊绊,因为我不确定如何从直方图中获取最大值和最小值。这是否可能,或者我是否必须采取另一种方式?我很失落。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

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

histValues = hist(sumOfRoll, Bins);

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

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