# Is there a function for creating n (nearly) evenly-spaced integers for indexing?

**URL:** <https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744>\
**Category:** New to Julia\
**Tags:** indexing\
**Created:** [January 12, 2024, 8:07pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744 "2024-01-12T20:07:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![parb](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@parb](https://discourse.julialang.org/u/parb)\
**Post date:** [January 12, 2024, 8:07pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/1 "2024-01-12T20:07:31Z")

</div>

Hi all,

I’d like to downsample arrays with something like

```julia
longarray = rand(100)
numsamples = 30
sampleindices = evenintegers(1, length(longarray), numsamples)
downsampled = longarray[sampleindices]

```

I realize its not obvious what “even” means here, since it cannot give the same exact difference between the integers, and its not clear if the first and last integer should be included.

What I do now instead is this, which feels like I’m overlooking some built-in functionality.

```julia
sampleindices = convert.(
    Int,
    round.(range(1, length(longarray), numsamples), digits=0
)

```

Can I reduce these last three lines to a single statement using built-in Julia?

Thanks!

---

<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:** [January 12, 2024, 8:30pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/2 "2024-01-12T20:30:12Z")

</div>

Those three lines could be written shortly:

```julia
round.(Int, range(1, length(longarray), numsamples))

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 12, 2024, 8:32pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/3 "2024-01-12T20:32:03Z")

</div>

```julia
julia> collect(Iterators.partition(1:37, 5))
8-element Vector{UnitRange{Int64}}:
 1:5
 6:10
 11:15
 16:20
 21:25
 26:30
 31:35
 36:37

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 12, 2024, 8:33pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/4 "2024-01-12T20:33:59Z")

</div>

```julia
round.(Int,LinRange(1,100,30))

```

shorter but basically the same.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 12, 2024, 8:43pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/5 "2024-01-12T20:43:27Z")

</div>

> [@adienes](#):
>
> `julia> collect(Iterators.partition(1:37, 5))`

For what the OP wants here, I think you’d actually want to write

```julia
(first(chunk) for chunk in Iterators.partition(longarray, numsamples))

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 12, 2024, 8:44pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/6 "2024-01-12T20:44:00Z")

</div>

> [@rafael.guerra](#):
>
> `round.(Int, range(1, length(longarray), numsamples))`

Even better: put this into the array indexing expression so you can use `begin` and `end` directly:

```Julia
A[round.(Int, range(begin, end, numsamples))]

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 12, 2024, 8:52pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/7 "2024-01-12T20:52:05Z")

</div>

`firstindex(xs)` and `lastindex(xs)` can be used instead of `1` and `length(xs)` to avoid problems with OffsetArrays.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 12, 2024, 8:59pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/8 "2024-01-12T20:59:13Z")

</div>

Maybe you are looking for `chunks` from ChunkSplitters.jl package?

The docs are here:

> **[Home · ChunkSplitters.jl](https://m3g.github.io/ChunkSplitters.jl/stable/)**
>
> Documentation for ChunkSplitters.jl.

Use it perhaps as (there is much more in the docs):

```julia
julia> using ChunkSplitters

julia> map(first∘first, chunks(longarray, 30, :batch))
30-element Vector{Int64}:
  1
  5
  9
 13
 17
 21
 25
 29
 33
 37
  ⋮
 92
 95
 98

```

might be the goal?

The indices can then be used as:

```julia
longarray[map(first∘first, chunks(longarray, 30, :batch))]

```

This allows a completely independent sample to be produced as:

```julia
using ChunkSplitters
using IterTools: nth
longarray[map(Base.Fix2(nth, 2)∘first, chunks(longarray, 30, :batch))]

```

---

<div class="post-metadata">

**Author:** ![parb](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@parb](https://discourse.julialang.org/u/parb)\
**Post date:** [January 12, 2024, 9:08pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/9 "2024-01-12T21:08:21Z")

</div>

The replies by @oheil, @rafael.guerra, and @mbauman are nice because they use Base functions and look simple to an occasional programmer, but I guess the answer is effectively no: There’s no (single) function to index my long arrays evenly at n points.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 12, 2024, 9:09pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/10 "2024-01-12T21:09:19Z")

</div>

what’s wrong with `Iterators.partition` ? it’s almost exactly what you’re looking for

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 12, 2024, 9:13pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/11 "2024-01-12T21:13:16Z")

</div>

How so? It’s just breaking the array up into chunks, not sampling it.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 12, 2024, 9:14pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/12 "2024-01-12T21:14:55Z")

</div>

> [@parb](#):
>
> they use Base functions and look simple to an occasional programmer

Julia trying to spread the simple functions to more packages and not have a Base monolith (AFAIK as I’m not a core-dev).

So the ChunkSplitters, IterTools usage I’ve mentioned looks pretty nice to Julians.

And can be made into a simple one-line function if needed. The name of your choice.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 12, 2024, 9:22pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/13 "2024-01-12T21:22:11Z")

</div>

I mean, you can partition `eachindex`. I guess it’s not an exact match

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 12, 2024, 9:29pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/14 "2024-01-12T21:29:37Z")

</div>

Another nice combination with ChunkSplitters:

```julia
using ChunkSplitters
longarray = rand(100);

indices = first(getchunk(longarray, 1, length(longarray)÷30, :scatter),30)
samples = longarray[indices]

```

This method is also quite efficient (as the indices returned from `getchunk` and `first` are integer ranges).

---

<div class="post-metadata">

**Author:** ![parb](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@parb](https://discourse.julialang.org/u/parb)\
**Post date:** [January 12, 2024, 9:30pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/15 "2024-01-12T21:30:49Z")

</div>

Sure - I certainly don’t wanna speak for Julians. I’ll speak for my future self ~3 months from now when I next code something and need to down-sample a long array: I’m not gonna remember the composition of `first` with `first`, nor what the right parameters were for `chunks`.

I was hoping for a simple builtin because that’s something I might remember. In Python’s numpy I remember it, because it’s “just” a call to “as type Int” on the linspace.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 12, 2024, 9:35pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/16 "2024-01-12T21:35:38Z")

</div>

If you only need one such subsample (and not several), I think this might be good (though not superefficient because of randomness):

```julia
using StatsBase
sample(longarray, 30; replace=false)

```

and as the Romans use to say:

> if you haven’t imported StatsBase, you haven’t done anything.

Okay, joke aside, the above is not a downsample, so maybe:

```julia
longarray[first(1:length(longarray)÷30:end, 30)]

```

which is short enough to remember and in Base.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 13, 2024, 1:26pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/17 "2024-01-13T13:26:08Z")

</div>

Isn’t just

```julia
longarray[begin:30:end]

```

enough here? (Maybe just adjusting the step if the number of samples is given)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [January 13, 2024, 2:36pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/18 "2024-01-13T14:36:59Z")

</div>

This is also what I expected to see suggested

---

<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:** [January 13, 2024, 2:39pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/19 "2024-01-13T14:39:57Z")

</div>

> [@lmiq](#):
>
> enough here?

It doesn’t seem to be enough as OP wants 30 indices nearly evenly distributed, while that will give him only 4.

---

<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:** [January 13, 2024, 9:30pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744/20 "2024-01-13T21:30:22Z")

</div>

```julia
A[range(start=begin, step=end ÷ 30, length=30)]

```

[Next page](https://discourse.julialang.org/t/is-there-a-function-for-creating-n-nearly-evenly-spaced-integers-for-indexing/108744.md?page=2)
