# Indexing an array with rounded array of floating point indexes

**URL:** <https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639>\
**Category:** General Usage\
**Created:** [July 24, 2020, 9:10pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639 "2020-07-24T21:10:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 24, 2020, 9:10pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/1 "2020-07-24T21:10:49Z")

</div>

I have:

```julia
a = rand(Int32, 10, 10)
i = Float32[8.47, 8.47]

```

and I want access an element `e` of `a` using rounded values in `i` as 2D index. Is there a more concise way of doing it than:

```julia
j = Int.(round.(i))
e = a[j[1], j[2]]

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [July 24, 2020, 9:25pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/2 "2020-07-24T21:25:52Z")

</div>

There is

```julia
a[round.(Int, i)...]

```

Performance-wise, though,

```julia
a[round.(Int, (i[1], i[2]))...]

```

would be better, as splatting can be done at compile time.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 25, 2020, 7:51am UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/3 "2020-07-25T07:51:21Z")

</div>

I think the compiler can figure this out just fine if you use eg `map`:

```julia
julia> using BenchmarkTools

julia> round_getindex(a, ι) = a[map(i -> round(Int, i), ι)...]
round_getindex (generic function with 1 method)

julia> round_getindex_dot(a, ι) = a[round.(Ref(Int), ι)...]
round_getindex_dot (generic function with 1 method)

julia> round_getindex2(a, ι) = a[round(Int, ι[1]), round(Int, ι[2])]
round_getindex2 (generic function with 1 method)

julia> a = rand(Int, 100, 100);

julia> ι = (1.1, 3.9)
(1.1, 3.9)

julia> @btime round_getindex($a, $ι)
  5.027 ns (0 allocations: 0 bytes)
9096069683632360821

julia> @btime round_getindex_dot($a, $ι)
  102.631 ns (3 allocations: 48 bytes)
9096069683632360821

julia> @btime round_getindex2($a, $ι)
  5.028 ns (0 allocations: 0 bytes)
9096069683632360821

```

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 25, 2020, 4:39pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/4 "2020-07-25T16:39:25Z")

</div>

> [@Tamas\_Papp](#):
>
> `ι = (1.1, 3.9)`

It runs faster because you are using tuples. My case requires an array, though. Here is what I get:

```julia
a = rand(Int, 100, 100)
i = [1.1, 3.9]

round_getindex(a, ι) = a[map(i -> round(Int, i), ι)...]
round_getindex_dot_(a, ι) = a[round.(Int, ι)...]
round_getindex_dot(a, ι) = a[round.(Ref(Int), ι)...]
round_getindex2(a, ι) = a[round(Int, ι[1]), round(Int, ι[2])]

julia> @btime round_getindex($a, $i)
  86.435 ns (4 allocations: 144 bytes)
-1777074699498022323

julia> @btime round_getindex_dot_($a, $i)
  78.898 ns (3 allocations: 128 bytes)
-1777074699498022323

julia> @btime round_getindex_dot($a, $i)
  282.062 ns (10 allocations: 288 bytes)
-1777074699498022323

julia> @btime round_getindex2($a, $i)
  3.363 ns (0 allocations: 0 bytes)
-1777074699498022323

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 25, 2020, 5:42pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/5 "2020-07-25T17:42:38Z")

</div>

> [@pauljurczak](#):
>
> My case requires an array, though.

If feasible, consider `StaticArrays.SVector` if speed is relevant.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 25, 2020, 5:46pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-rounded-array-of-floating-point-indexes/43639/6 "2020-07-25T17:46:48Z")

</div>

Fortunately, speed is not a problem. I’m constrained by Makie API:

```julia
pos = to_world(scene1, Point2f0(scene1.events.mouseposition[]))

```

My main objective was to learn a new syntax trick. 🙂
