从MatLab中的直方图中获取最大和最小图表值(Obtaining the Maximum and Minimum Graphed Values from a Histogram in MatLab)

编程入门 行业动态 更新时间:2024-10-18 16:52:39
从MatLab中的直方图中获取最大和最小图表值(Obtaining the Maximum and Minimum Graphed Values from a Histogram in MatLab)

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

模拟滚动两个6面模具N次的实验,将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'); 
  
 

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

I have been given the task in MatLab of creating a program that:

Simulates the experiment of rolling two 6-sided dies N number of times, summing the 2 values rolled, and then graphing the frequency in which those values were obtained. Is able to then print the percentage difference between the most frequent and least frequent of the rolled outcomes.

I have already figured out how to do the first part:

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

I'm stumbling on how to achieve the 2nd part, because I'm uncertain how to obtain the maximum and minimum values from my histogram. Is this even possible, or do I have to go about it another way? I'm quite lost. Any help would be amazing.

最满意答案

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

histValues = hist(sumOfRoll, Bins);

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

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

You can just modify your existing code to assign the histogram values to a variable and use it to find the percentage difference.

histValues = hist(sumOfRoll, Bins);

Here, histValues holds the values of histogram for each bin. Then, you can use these values to figure out the difference and percentage difference.

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

更多推荐

本文发布于:2023-08-07 15:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:直方图   图表   大和   最小   MatLab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!