# The bin2 function

**URL:** <https://discourse.julialang.org/t/the-bin2-function/70488>\
**Category:** General Usage\
**Tags:** tullio\
**Created:** [October 27, 2021, 3:03pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488 "2021-10-27T15:03:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [October 27, 2021, 3:03pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/1 "2021-10-27T15:03:24Z")

</div>

I am looking at a code of an early version of Julia and there is this function, `bin2()` that I don’t know what is it for specially I can’t see the output how can I write in Julia 1.5 ?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 27, 2021, 3:09pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/2 "2021-10-27T15:09:38Z")

</div>

Neither `bin2` nor `Base.bin2` exist in Julia 0.4, 0.6, or 0.7. Maybe that function is coming from some package?

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [November 1, 2021, 3:37pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/3 "2021-11-01T15:37:00Z")

</div>

I tried to see if it’s from a package but I couldn’t tell

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 1, 2021, 3:50pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/4 "2021-11-01T15:50:46Z")

</div>

Binning of a 2D array [as in this post](https://discourse.julialang.org/t/binning-a-2d-array/46791/9)?

(_similar purpose_ [`bin2()`](https://www.rdocumentation.org/packages/ash/versions/1.0-15/topics/bin2) _in R-documentation_)

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 1, 2021, 4:23pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/5 "2021-11-01T16:23:34Z")

</div>

The fastest/simplest solution I found is:

```julia
julia> using Tullio

julia> x = [i*4 + j for i=0:3, j=1:4]
4×4 Matrix{Int64}:
  1 2 3 4
  5 6 7 8
  9 10 11 12
 13 14 15 16

julia> bin2(x) = @tullio res[i,j] := x[2i,2j] + x[2i,2j-1] + x[2i-1, 2j] + x[2i-1, 2j-1]
bin2 (generic function with 1 method)

julia> bin2(x)
2×2 Matrix{Int64}:
 14 22
 46 54

julia> y = [i*5 + j for i=0:4, j=1:5]
5×5 Matrix{Int64}:
  1 2 3 4 5
  6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

julia> bin2(y)
2×2 Matrix{Int64}:
 16 24
 56 64

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 1, 2021, 6:43pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/6 "2021-11-01T18:43:38Z")

</div>

Not as fast as the Tullio example but, fwiw, a good general solution can be found [in this post](https://discourse.julialang.org/t/there-is-an-analog-of-numpy-reduceat-in-julia/70187/4).

Do you know if it is possible to generalize Tulio’s approach for `k x k` bin sizes?

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 1, 2021, 6:45pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/7 "2021-11-01T18:45:10Z")

</div>

As far as I know only via metaprogramming.

I did that once for n dimensional regularizers with different step sizes.

See [this issue](https://github.com/mcabbott/Tullio.jl/issues/11).

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [November 1, 2021, 9:06pm UTC](https://discourse.julialang.org/t/the-bin2-function/70488/8 "2021-11-01T21:06:46Z")

</div>

thanks, guys now I know how to do it and what does it exactly means.
