# How to get vector from set of ranges?

**URL:** <https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571>\
**Category:** General Usage\
**Created:** [May 24, 2022, 10:32am UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571 "2022-05-24T10:32:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [May 24, 2022, 10:32am UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/1 "2022-05-24T10:32:45Z")

</div>

I am trying to create a vector using in-line for loop.

```julia
indices = [2,5,6,8]
[i:i+6; for i in indices]

```

which returns

> 4-element Vector{UnitRange{Int64}}:  
> 2:8  
> 5:11  
> 6:12  
> 8:14

How to create vector of values instead of ranges?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 24, 2022, 10:39am UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/2 "2022-05-24T10:39:46Z")

</div>

You mean like this?

```julia
julia> reduce(vcat, [i:i+6 for i in indices])
28-element Vector{Int64}:
  2
  3
  4
  5
  6
  7
  8
  5
  6
(...)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 24, 2022, 10:55am UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/3 "2022-05-24T10:55:48Z")

</div>

Sorry actually you don’t even need the array comprehension as `reduce` accepts a generator, so this will likely be more efficient:

```julia
reduce(vcat, i:i+6 for i in indices)

```

---

<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:** [May 24, 2022, 3:32pm UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/4 "2022-05-24T15:32:48Z")

</div>

another way to —

```julia
vcat((:).(indices,indices.+6)...)

```

---

<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:** [May 24, 2022, 3:46pm UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/5 "2022-05-24T15:46:44Z")

</div>

Something like this should work and may be faster yet:

```julia
[i+j for j in 0:6 for i in indices]

```

(not tested, I may get the index order wrong).

(But I guess reduce (vcat,…) does not allocate any intermediate either?)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 24, 2022, 3:57pm UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/6 "2022-05-24T15:57:44Z")

</div>

I see:

```julia
julia> @btime reduce(vcat, i:i+6 for i in $indices)
  156.910 ns (9 allocations: 880 bytes)

```

and

```julia
julia> @btime [i+j for j in 0:6 for i in $indices]
  175.882 ns (4 allocations: 544 bytes)

```

🤷 (yours does get the indices wrong)

It also seems like the `reduce` trick actually doesn’t help here?

```julia
julia> @btime vcat([i:i+6 for i in $indices]...)
  136.101 ns (6 allocations: 544 bytes)

```

EDIT actually I only tried this because my subconscious reminded me that Frames was griping about this issue the other day:

[https://github.com/JuliaLang/julia/issues/31636](https://github.com/JuliaLang/julia/issues/31636)

---

<div class="post-metadata">

**Author:** ![abobroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abobroff/32/3752_2.png) [@abobroff](https://discourse.julialang.org/u/abobroff)\
**Post date:** [May 24, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/7 "2022-05-24T17:59:36Z")

</div>

🙂 matlab influence:

`julia> ([indices...;;] .+ [0:6...])[:]`

---

<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:** [May 24, 2022, 6:55pm UTC](https://discourse.julialang.org/t/how-to-get-vector-from-set-of-ranges/81571/8 "2022-05-24T18:55:45Z")

</div>

Just for completeness:

```julia
julia> inds = indices = [2,5,6,8]
4-element Vector{Int64}:
 2
 5
 6
 8

julia> @btime reduce(vcat, i:i+6 for i in $inds );
  208.478 ns (9 allocations: 880 bytes)

julia> @btime [i+j for i in $inds for j in 0:6];
  240.907 ns (4 allocations: 544 bytes)

julia> function f(inds, range)
           v = Int[]
           for i in inds
               for j in range
                   push!(v, i+j)
               end
           end
           return v
       end
f (generic function with 2 methods)

julia> @btime f($inds, 0:6);
  199.283 ns (3 allocations: 480 bytes)

julia> function f2(inds, range)
           v = Vector{Int}(undef, length(range)*length(inds)) 
           ii = 0
           for i in inds
               for j in range
                 ii += 1
                 v[ii] = i + j  
               end
           end
           return v
       end
f2 (generic function with 1 method)

julia> @btime f2($inds, 0:6);
  47.373 ns (1 allocation: 288 bytes)

```

The only thing I don’t “like” is that `f` is faster than the comprehension, which I thought would be lowered to the same thing.
