How do I make a histogram using value - count pairs?

15 views (last 30 days)
I have a 50027x2 matrix, M.
M(line, 2) is the amount of times the value M(line, 1) appeared on my problem.
In other words, instead of making a histogram using an array like [1 1 1 1 2 2 2 3], I'd like to get the same result through a matrix that looks like this:
[1 4;
2 3;
3 1]
I can plot it just fine, but for my assignment I need to make a histogram. Converting the matrix into an array by repeating the elements is not possible, since, for my M matrix, I'd need an array of 6,654,651,016 elements.

Accepted Answer

dpb
dpb on 29 Dec 2020
Edited: dpb on 29 Dec 2020
histogram('categories',categorical(M(:,1)),'bincounts',M(:,2))
to match the number of elements in x,y.
Otherwise, have to set the 'BinEdges' vector and it has to have length one greater than length of the counts vector.
See
doc histogram
for more details.
  7 Comments
dpb
dpb on 30 Dec 2020
Hmmm....both worked here. Oh! Headslap! I used a test case with evenly divisible number of elements; if not have to round the divisor to have an integer spacing--
hAx.XTick=hAx.XTick([1:ceil(numel(hAx.XTick)/10):end end]);
Side note: In future please cut 'n paste the text of the message from the screen instead of using screenshots -- then folks don't have to go look somewhere else to see the message...
Tomé Carvalho
Tomé Carvalho on 30 Dec 2020
Thanks, it worked now. Hopefully there won't be a next time!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!