How to draw probability density function in MatLab?

后端 未结 4 1845
花落未央
花落未央 2021-01-03 00:13
x = [1 2 3 3 4]
cdfplot(x)

After Googling, I find the above code will draw a cumulative distribution function for me in Matlab.
Is there a simp

4条回答
  •  生来不讨喜
    2021-01-03 00:44

    You can generate a discrete probability distribution for your integers using the function hist:

    data = [1 2 3 3 4];           %# Sample data
    xRange = 0:10;                %# Range of integers to compute a probability for
    N = hist(data,xRange);        %# Bin the data
    plot(xRange,N./numel(data));  %# Plot the probabilities for each integer
    xlabel('Integer value');
    ylabel('Probability');
    

    And here's the resulting plot:

    enter image description here


    UPDATE:

    In newer versions of MATLAB the hist function is no longer recommended. Instead, you can use the histcounts function like so to produce the same figure as above:

    data = [1 2 3 3 4];
    N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf');
    plot(N);
    xlabel('Integer value');
    ylabel('Probability');
    

提交回复
热议问题