# How would I separate a vector into groups where the values are close?

**URL:** <https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988>\
**Category:** New to Julia\
**Created:** [September 9, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988 "2022-09-09T13:44:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [September 9, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/1 "2022-09-09T13:44:31Z")

</div>

Say I have a vector

```julia
v = [vcat(1:10); vcat(24:4:40); vcat(500:10:560); vcat(1000:2:1020)]

```

The values have a wide range but there are groups where the values are close to each other. How would I seperate these groups into individual vectors?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 9, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/2 "2022-09-09T13:48:13Z")

</div>

It depends on what you mean by “close”. Do you have a specific rule in mind?

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [September 9, 2022, 1:49pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/3 "2022-09-09T13:49:35Z")

</div>

Some number. In this case 11.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 9, 2022, 1:54pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/4 "2022-09-09T13:54:36Z")

</div>

Sorry, 11 what? I don’t understand

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 9, 2022, 1:57pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/5 "2022-09-09T13:57:20Z")

</div>

```julia
julia> function splitgroups(v)
         start = firstindex(v)
         groups = typeof(v)[]
         for stop in [findall(diff(v) .> 10); lastindex(v)]
           push!(groups, @view(v[start:stop]))
           start = stop + 1
         end
         groups
       end
splitgroups (generic function with 1 method)

julia> splitgroups(v)
4-element Vector{Vector{Int64}}:
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 [24, 28, 32, 36, 40]
 [500, 510, 520, 530, 540, 550, 560]
 [1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018, 1020]

```

This checks for places where the difference between numbers becomes greater than 10, and splits at those places.

It avoids allocating memory for the new vectors by using `@view`, so the original vector `v` will be changed if the contents of these change. If the original vector `v` also will still be used, and you want them to be independent of each other, you can remove the `@view` at the cost of some performance.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [September 9, 2022, 2:05pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/6 "2022-09-09T14:05:43Z")

</div>

Thank you, that’s exactly it. But you missed the last group?

```julia
function splitgroups(v)  
  start = firstindex(v)
  groups = typeof(v)[]
  for stop in findall(diff(v) .> 10)
    push!(groups, @view(v[start:stop]))
    start = stop + 1
  end
  push!(groups, @view(v[start:end]))
  groups
end

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 9, 2022, 2:13pm UTC](https://discourse.julialang.org/t/how-would-i-separate-a-vector-into-groups-where-the-values-are-close/86988/7 "2022-09-09T14:13:23Z")

</div>

Oh, right. I’ve edited the code to fix it, but your way also works. The way I’ve done it does a small allocation for the sake of avoiding code duplication (of the `push!`) line, which is the way I prefer it.

_Edit_: You can leave your post also up, it would be useful as a slightly different solution for others who might need it.
