Say I have a vector
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?
Say I have a vector
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?
It depends on what you mean by “close”. Do you have a specific rule in mind?
Some number. In this case 11.
Sorry, 11 what? I don’t understand
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.
Thank you, that’s exactly it. But you missed the last group?
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
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.