how to calculate the binomial distribution in matlab

The binomial distribution can be calculated in matlab using the binopdf function which returns the probability mass function (pmf) value for a given number of trials and successes, given the probability of success in each trial.

Here's an example code snippet to calculate the binomial distribution for 10 trials with probability of success as 0.2, and then finding the probability of getting exactly 3 successes.

main.m
% Number of trials
n = 10;

% Probability of success in each trial
p = 0.2;

% Find probability of getting exactly 3 successes
k = 3;
prob = binopdf(k, n, p);

disp(['Probability of getting exactly 3 successes = ', num2str(prob)]);
232 chars
12 lines

The output of this code will be:

Probability of getting exactly 3 successes = 0.201326592

gistlibby LogSnag