zelanix
zelanix

Reputation: 3562

Plot histogram from vector of counts

If I have a vector of histogram edges and bin counts, is it possible to use this to directly generate a histogram plot?

For example, given data vectors

edges = 0:10; % histogram edges for 9 bins
counts = round(normpdf(edges(1:end-1), 5, 2) * 1000) % Generate bin counts

counts =

     9    27    65   121   176   199   176   121    65    27

I can always generate the data artificially as

data = [];
for i = 1:numel(counts)
  % This should be optimised by pre-allocating the data array,
  % but this is only provided as an example.
  data = [data (ones(1, counts(i)) * mean(edges(i:i+1)))];
end

so that numel(data) == sum(counts) and I can then plot the histogram using histogram(data, edges):

enter image description here

However, I would like to do this without having to do the intermediary step of generating the artificial data as this seems rather convoluted.

I know that I could use the bar function, but I would prefer to use histogram because I prefer the way that it plots and the functionality that it offers.

Edit: I'm using MATLAB R2015a / R2015b, although I would prefer to maintain backward compatibility with R2015a if possible (I know that there were fairly major changes to histogram in R2015b).

Upvotes: 2

Views: 2089

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

I don't think you can circumvent feeding the actual data to a Histogram object. The functionality of the Histogram object includes being able to change bins/edges after the fact, and for that it needs to know its source data.

Upvotes: 1

Related Questions