# Hist() and hits() in Julia?

**URL:** https://discourse.julialang.org/t/hist-and-hits-in-julia/1224
**Category:** Statistics
**Tags:** question
**Created:** [December 30, 2016, 11:03pm UTC](https://discourse.julialang.org/t/hist-and-hits-in-julia/1224 "2016-12-30T23:03:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mgiugliano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgiugliano/32/5818_2.png) [@mgiugliano](https://discourse.julialang.org/u/mgiugliano)
#### Post date: [December 30, 2016, 11:03pm UTC](https://discourse.julialang.org/t/hist-and-hits-in-julia/1224/1 "2016-12-30T23:03:08Z")

</div>

Given a set of elements, I would love to efficiently partition their numerical values into bins, and then return the count in each bin, as well as the bin edges.

MATLAB used to have super simple functions for this (i.e. hist, and histc). I wrote my own (quick and dirty) code. Which is the best (and simplest) way to do it ?

Note: I don’t care about plotting - I need to do subsequent analysis on the bins and the counts.  
Thanks for your patience and help.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [December 30, 2016, 11:14pm UTC](https://discourse.julialang.org/t/hist-and-hits-in-julia/1224/2 "2016-12-30T23:14:20Z")

</div>

You can use [StatsBase](https://github.com/JuliaStats/StatsBase.jl) for this

```julia
julia> using StatsBase

julia> result = fit(Histogram, randn(1000))
StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}}
edges:
  -4.0:1.0:4.0
weights: [2,24,131,361,342,129,10,1]
closed: right

```

Replace the `randn(1000)` in the code above with the vector your are working with.  
You can access the properties of the result using `result.edges` and `result.weights`.

See [http://statsbasejl.readthedocs.io/en/latest/empirical.html](http://statsbasejl.readthedocs.io/en/latest/empirical.html)

---

<div class="post-metadata">

### Author: ![mgiugliano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgiugliano/32/5818_2.png) [@mgiugliano](https://discourse.julialang.org/u/mgiugliano)
#### Post date: [December 30, 2016, 11:45pm UTC](https://discourse.julialang.org/t/hist-and-hits-in-julia/1224/3 "2016-12-30T23:45:26Z")

</div>

Tnx!!! [Documentation](http://statsbasejl.readthedocs.io/en/latest/empirical.html?highlight=Histogram) is unfortunately ultra-condensed.

I failed to understand how to run a simple test against the theory

data = randn(10000);

x = -10:0.1:10;  
y = 1./sqrt(2π) \* exp(-0.5\*x.^2);

Anyway, thank you for your reply and competent advise.  
M
