The bin2 function

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 ?

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

5 Likes

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

Binning of a 2D array as in this post?

(similar purpose bin2() in R-documentation)

1 Like

The fastest/simplest solution I found is:

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
2 Likes

Not as fast as the Tullio example but, fwiw, a good general solution can be found in this post.

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

1 Like

As far as I know only via metaprogramming.

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

See this issue.

2 Likes

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