CerebralCortexan
CerebralCortexan

Reputation: 257

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

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

  1. 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.
  2. 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');

enter image description here

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.

Upvotes: 0

Views: 1023

Answers (1)

nahomyaja
nahomyaja

Reputation: 202

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

Upvotes: 1

Related Questions