【问题标题】:Obtaining the Maximum and Minimum Graphed Values from a Histogram in MatLab在 MatLab 中从直方图中获取最大和最小图形值
【发布时间】:2017-01-26 15:46:47
【问题描述】:

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

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

【问题讨论】:

  • 1) 不要使用histhistogram 在现代版本的matlab 中是首选。 2) 看看histcounts

标签: matlab max histogram minimum


【解决方案1】:

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

histValues = hist(sumOfRoll, Bins);

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

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

【讨论】:

  • 非常感谢!没想到这么简单。
猜你喜欢
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2018-04-01
  • 2017-01-04
  • 2019-01-24
相关资源
最近更新 更多