MATLAB: How to plot distribution curves of two vectors

histogramstatistics

I have two vectors, charge 'q' and corresponding phase angle 'Ph', each of size 89905×1. 'Ph'- min=0 degree, max=360 degree & 'q'- min=-4e-12, max=4.5e-12. I want to plot distribution curves, in which the x-axis is my phase angle vector which is divided into small phase windows of width,say 1.5 degree and y axis : 1. number of charges present in that phase window. 2. sum of charge values observed in phase window. 3. average value of charge value observed in phase window. 4. maximum value of charge observed in phase window.

Best Answer

Use discretize to build your bins and accumarray for the statistics of each bin:
bins = 0:1.5:360;
binindex = discretize(Ph, bins);
binstarts = bins(1:end-1)'; %last bin includes both edges. See discretize doc
bincount = accumarray(binindex, q, size(binstarts), @numel); %number of charge per bin
binsum = accumarray(binindex, q, size(binstarts)); %sum of charge per bin (default function of accumarray)
binmean = accumarray(binindex, q, size(binstarts), @mean); %average of charge
binmax = accumarray(binindex, q, size(binstarts), @max); %max of charge
plot(binstarts, [bincount, binsum, binmean, binmax])