Search code examples
matlabprobabilityprobability-density

How can I define Y to be different for different X values like a histogram


I'm trying to plot a probability mass function for the probability of certain sums when rolling three dices and I found this example in MathCad and wondered if there is anything like it in MatLab?

Function like I'm searching for in MathCad Prime


Solution

  • I imagine you’d build that as a lookup table rather than a series of if/else statements. It is easy to compute the probabilities using convolution:

    f1 = ones(1,6);
    f2 = conv(f1,f1);
    f = conv(f2,f1);
    % because f contains values from x=3 to 18,
    % rather than starting at 1 as MATLAB arrays do,
    % we add two zeros to the front:
    f = [0,0,f];
    

    Now f(x) returns the probability of throwing x.