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

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.

 採用された回答

dpb
dpb 2020 年 12 月 29 日
編集済み: dpb 2020 年 12 月 29 日

2 投票

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 件のコメント

Tomé Carvalho
Tomé Carvalho 2020 年 12 月 29 日
I take it you meant M(:,2) on the last argument, right?
I tried that, and it looks like the plot but cursed at the bottom and the top, why is that?
dpb
dpb 2020 年 12 月 29 日
編集済み: dpb 2020 年 12 月 29 日
Yeah, sorry about not fixing the typo on column
With so many points there are way too many categories to put on the axes for ticks and apparently histogram wasn't smart enough on its own to only put on a few instead of trying to do them all. That's a quality of implementation issue worthy of a bug report.
Try something like
histogram('categories',categorical(M(:,1)),'bincounts',M(:,2))
hAx=gca;
hAx.XTick=hAx.XTick([1:size(M,1)/10:end end]);
to only put 10 tick marks plus the end one on the x axis.
Tomé Carvalho
Tomé Carvalho 2020 年 12 月 29 日
That actually gave me this error
dpb
dpb 2020 年 12 月 30 日
Oh, yeah. I used a variable in which M(:,1) was unique; yours undoubtedly isn't so aren't as many categories as values. Use the size of the XTick array instead...
hAx.XTick=hAx.XTick([1:numel(hAx.XTick)/10:end end]);
Tomé Carvalho
Tomé Carvalho 2020 年 12 月 30 日
My M(:, 1) is unique (assuming you mean each of its values appears only once). I tried the line you just gave me and I still get the same error.
dpb
dpb 2020 年 12 月 30 日
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 2020 年 12 月 30 日
Thanks, it worked now. Hopefully there won't be a next time!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

リリース

R2020b

質問済み:

2020 年 12 月 29 日

コメント済み:

2020 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by