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

15 vues (au cours des 30 derniers jours)
Tomé Carvalho
Tomé Carvalho le 29 Déc 2020
Commenté : Tomé Carvalho le 30 Déc 2020
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.

Réponse acceptée

dpb
dpb le 29 Déc 2020
Modifié(e) : dpb le 29 Déc 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 commentaires
dpb
dpb le 30 Déc 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 le 30 Déc 2020
Thanks, it worked now. Hopefully there won't be a next time!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by