# Type of array index?

**URL:** <https://discourse.julialang.org/t/type-of-array-index/76065>\
**Category:** New to Julia\
**Created:** [February 9, 2022, 8:37am UTC](https://discourse.julialang.org/t/type-of-array-index/76065 "2022-02-09T08:37:17Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 17, 2023, 1:11pm UTC](https://discourse.julialang.org/t/type-of-array-index/76065/22 "2023-08-17T13:11:55Z")

</div>

```julia
using InvertedIndices
 function partition_vec1(v, thresh)
    idx_l = findall(<(thresh), v)
    (idx_l, Not(idx_l))
  end

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [August 17, 2023, 3:14pm UTC](https://discourse.julialang.org/t/type-of-array-index/76065/23 "2023-08-17T15:14:38Z")

</div>

Interesting, but the use of `Not(idx_l)` is too much limited:

```julia
julia> v = rand(1:10, 10)
# . . . 
julia> a, b = partition_vec1(v, 7)
([1, 2, 3, 5, 7, 8, 10], InvertedIndex{Vector{Int64}}([1, 2, 3, 5, 7, 8, 10]))

julia> a
7-element Vector{Int64}:
  1
  2
  3
  5
  7
  8
 10

julia> b
InvertedIndex{Vector{Int64}}([1, 2, 3, 5, 7, 8, 10])

julia> collect(b)
ERROR: MethodError: no method matching length(::InvertedIndex{Vector{Int64}})

```

The `InvertedIndex` object itself doesn’t know what it’s the inverse of. As a result, you can use it only as an index into the same-sized array.

I didn’t say this in my original post, but I need to use both `idx_l` and `idx_h` independently of the original array, as

```julia
for i in idx_h

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 24, 2023, 11:49pm UTC](https://discourse.julialang.org/t/type-of-array-index/76065/24 "2023-08-24T23:49:07Z")

</div>

> [@ryofurue](#):
>
> I didn’t say this in my original post, but I need to use both `idx_l` and `idx_h` independently of the original array, as

You can do `b = setdiff(eachindex(v), a)` if you prefer.  
An allocation free way would be `b = Iterators.filter(∉(a), eachindex(v))`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 25, 2023, 6:23am UTC](https://discourse.julialang.org/t/type-of-array-index/76065/25 "2023-08-25T06:23:32Z")

</div>

Having now actually read the whole thread (sorry for contributing so irresponsibly before 😅 )  
if you only need the partitioned arrays for iteration, you may prefer not allocating them at all:

```julia
function partition_indices(f, v)
    inds = eachindex(v)
    Iterators.filter(i -> f(v[i]), inds), Iterators.filter(i -> !f(v[i]), inds)
end

```

Technically, you’ll call `f` twice as many times this way, but that may very well be cheaper than allocating two arrays, depending on your use case.

If you `collect` the two iterators (not recommended), you will see they are what you want:

```julia
julia> v = rand(Bool, 10)'
1×10 adjoint(::Vector{Bool}) with eltype Bool:
 1 0 1 0 1 0 0 0 1 1

julia> collect.(partition_indices(>(0.5), v))
([1, 3, 5, 9, 10], [2, 4, 6, 7, 8])

```

As a bonus, the type of `eachindex` doesn’t really matter to you, as per your original question.

---

<div class="post-metadata">

**Author:** ![Andy\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy_zhang/32/210182_2.png) [@Andy\_Zhang](https://discourse.julialang.org/u/Andy_Zhang)\
**Post date:** [July 12, 2024, 10:59pm UTC](https://discourse.julialang.org/t/type-of-array-index/76065/26 "2024-07-12T22:59:46Z")

</div>

I have the same question and found this topic, and I found the answer in the Julia document. The answer to this topic is not very precise.

The official document answers your question in detail: [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-supported-index-types)

For “standard arrays” and “standard indices”

> In the expression `A[I_1, I_2, ..., I_n]`, each `I_k` may be a scalar index, an array of scalar indices, or an object that represents an array of scalar indices and can be converted to such by [`to_indices`](https://docs.julialang.org/en/v1/base/arrays/#Base.to_indices):
> 
> 1. A scalar index. By default this includes:
> - Non-boolean integers
> - [`CartesianIndex{N}`](https://docs.julialang.org/en/v1/base/arrays/#Base.IteratorsMD.CartesianIndex)s, which behave like an `N`-tuple of integers spanning multiple dimensions (see below for more details)

> [@GunnarFarneback](#):
>
> Generally you can use any subtype of `Integer` for doing the indexing.

You cannot use Boolean as the index.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [July 13, 2024, 2:37am UTC](https://discourse.julialang.org/t/type-of-array-index/76065/27 "2024-07-13T02:37:29Z")

</div>

> [@Andy\_Zhang](#):
>
> I have the same question and found this topic . . . The official document answers your question in detail . . .

That’s because my initial question wasn’t formulated well. Through this discussion, I learned that

1. my _ **real question** _ was _How to find out the type of the index of the given array?_ and
2. _ **the answer** _ was/is `keytype(v)`.

This was a better way to pose my question because you get a better solution to the same problem:

```julia
function partition_vals(vals, thresh)
  idx_l = Vector{keytype(vals)}()
  idx_h = Vector{keytype(vals)}()
  for i in eachindex(vals)
    (vals[i] < thresh) ? push!(idx_l, i) : push!(idx_h, i)
  end
  (idx_l, idx_h)
end

```

Why is it better than `Vector{Int}()` ? Because the above function works not only with Vectors but also with Dicts (and anything that implements indexing `[]`).

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [July 13, 2024, 6:26am UTC](https://discourse.julialang.org/t/type-of-array-index/76065/28 "2024-07-13T06:26:16Z")

</div>

Agreeing that a generic solution is better whenever possible. Seems that your function still has a bug though:

```julia-repl
julia> A = [1 2; 3 4];

julia> partition_vals(A, 3)`
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type CartesianIndex{2}

```

The reason being – as alluded to above – that `keytype` does not necessarily match the type of `eachindex`. So, a proper generic version of your function could either use

- `eltype(eachindex(vals))` together with iterating over `i in eachindex(vals)`
- or `keytype(vals)` together with iterating over `i in keys(vals)`.

Both should work for vectors, dicts and arrays. For the latter, the meaning of indices is slightly different though as a multi-dimensional array can be accessed either by a linear index, i.e., as provided by `eachindex`, or a multi-dimensional index, e.g., a `CartesianIndex`. Here is a small example:

```julia-repl
julia> A[1, 2] # multi-dimensional index
2

julia> A[CartesianIndex(1, 2)] # same as above
2

julia> A[3] # linear index
2

julia> eachindex(A) # gives linear indices
Base.OneTo(4)

julia> eltype(eachindex(A)) # type of linear indices, i.e., like typeof(first(eachindex(A)), but works for empty vectors
Int64

julia> keys(A) # gives multi-dimensional indices
CartesianIndices((2, 2))

julia> keytype(A) # type of keys
CartesianIndex{2}

```

[Previous page](https://discourse.julialang.org/t/type-of-array-index/76065.md?page=1)
