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.

 Respuesta aceptada

dpb
dpb el 29 de Dic. de 2020
Editada: dpb el 29 de Dic. de 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 comentarios

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 el 29 de Dic. de 2020
Editada: dpb el 29 de Dic. de 2020
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.
That actually gave me this error
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]);
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.
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...
Thanks, it worked now. Hopefully there won't be a next time!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 29 de Dic. de 2020

Comentada:

el 30 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by