# Convert axes(a) etc to StepRange with a step other than 1

**URL:** <https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451>\
**Category:** Internals & Design\
**Created:** [September 25, 2018, 5:38am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451 "2018-09-25T05:38:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 5:38am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/1 "2018-09-25T05:38:36Z")

</div>

I want to loop over a matrix that might have custom indices. I can use `axes(a)` to get these. But I need a step size other than 1.

Is there a simple, generic way of converting Base.OneTo to a StepRange with a step other than 1 besides just doing it manually?

It would be good to have a helper method for this, I can’t imagine why else you would need to convert anything to a step range _except_ to have a step size other than 1!

Something like this could convert `axes(a)` to StepRanges with a step size 2:

```julia
StepRange.(axes(a), 2)

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 25, 2018, 5:59am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/2 "2018-09-25T05:59:40Z")

</div>

@Per has the better solution.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 25, 2018, 6:02am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/3 "2018-09-25T06:02:02Z")

</div>

alternatively:

```julia
[first(x):3:last(x) for x in axes(a)]

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 6:06am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/4 "2018-09-25T06:06:53Z")

</div>

It’s somehow unsatisfying to have to use a list comprehension for the most common reason for this conversion…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 25, 2018, 6:10am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/5 "2018-09-25T06:10:07Z")

</div>

It’s possible to use a dot-broadcast instead of a list comprehension.

```julia
(x -> first(x):3:last(x)).(axes(a))

```

This is likely more efficient, as it results in a tuple instead of an array.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 25, 2018, 6:10am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/6 "2018-09-25T06:10:25Z")

</div>

```julia
julia> StepRange.(first.(axes(a)),3,last.(axes(a)))
(1:3:7, 1:3:10, 1:3:13)

julia> function steprange(a::AbstractArray, stepby)
           stops = axes(a)
           return StepRange.(first.(stops), stepby, last.(stops))
       end
steprange (generic function with 1 method)

julia> steprange(a, 3)
(1:3:7, 1:3:10, 1:3:13)
# ----

julia> a = fill(0, (8+1,12+1,16+1));axes(a)
(Base.OneTo(9), Base.OneTo(13), Base.OneTo(17))

julia> steps = (2,3,4)
(2, 3, 4)

julia> steprange(a, steps)
(1:2:9, 1:3:13, 1:4:17)

julia> collect.(ans)
([1, 3, 5, 7, 9], [1, 4, 7, 10, 13], [1, 5, 9, 13, 17])

```

```julia
julia> Base.StepRange(first::T, step::T, last::AbstractRange) where {T<:Integer} =
           StepRange(first, step, last.stop)

julia> StepRange(2, 5, Base.OneTo(17))
2:5:17

julia> collect(ans)
4-element Array{Int64,1}:
  2
  7
 12
 17

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 6:12am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/7 "2018-09-25T06:12:06Z")

</div>

Yeah these are better! I imagine a lot faster as well without the allocation.

It would still be nice to have a method that accepted any other range and a step size…

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 6:13am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/8 "2018-09-25T06:13:49Z")

</div>

Lets get that in base now 😉

Edit: well a `StepRange(x, step)` constructor that accepted OneTo and the stepsize, anyway

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 25, 2018, 6:24am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/9 "2018-09-25T06:24:55Z")

</div>

I’d rather have a functon `restep(x, step) = first(x):step:last(x)` that would work on any range, not just `Base.OneTo`.

But with really small, rarely used functions like this, it is sometimes better to write out the code explicitly each time, rather than memorizing a function name and what it does.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 6:31am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/10 "2018-09-25T06:31:09Z")

</div>

Totally, that’s what I meant. But you would probably have to write a few specific implementations for OneTo and Range etc unless they just `convert()`. Edit: no you probably wouldn’t…

I also agree in principle that keeping rare function overhead down is a good idea. But in this case there are very few reasons to ever manually use `StepRange(x)` and at least a few of reasons to use `StepRange(x, step)`, so I’m not sure that really applies.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 25, 2018, 7:01am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/11 "2018-09-25T07:01:13Z")

</div>

> [@Raf](#):
>
> Edit: no you probably wouldn’t…

Yeah, this is what I love about Julia.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 25, 2018, 7:54am UTC](https://discourse.julialang.org/t/convert-axes-a-etc-to-steprange-with-a-step-other-than-1/15451/12 "2018-09-25T07:54:09Z")

</div>

Hah yeah I’m still not used to it! So much useless boilerplate to unlearn.
