# Partitioning a vector

**URL:** <https://discourse.julialang.org/t/partitioning-a-vector/109511>\
**Category:** General Usage\
**Created:** [January 31, 2024, 1:11pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511 "2024-01-31T13:11:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 31, 2024, 1:11pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/1 "2024-01-31T13:11:37Z")

</div>

Dear all,  
I have a vector: `I = 1:55`. I need to partition it into 10 subvectors, none of which must be empty and the length of each of which is a random number. What is the best way to do this? Could someone help me?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 31, 2024, 1:26pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/2 "2024-01-31T13:26:28Z")

</div>

What is the use case? Do you have other constraints on the lengths? Why not take them constant?

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 31, 2024, 1:27pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/3 "2024-01-31T13:27:25Z")

</div>

I need different lenghts to insert in a optimization problem. Moreover, all components of ```I```` need to be used.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 31, 2024, 1:29pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/4 "2024-01-31T13:29:05Z")

</div>

Okay but why do the lengths need to be different?  
Do the subvectors need to be contiguous?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 31, 2024, 1:32pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/5 "2024-01-31T13:32:24Z")

</div>

Here’s a simple code that does uniform assignment over the `k` subvectors.

```julia
n, k = 55, 10
x = 1:n
select = rand(1:k, n)
subvectors = Vector{Vector{eltype(x)}}(undef, k)
for i in 1:k
    subvectors[i] = x[select .== i]
end

```

Example result:

```julia
julia> subvectors
10-element Vector{Vector{Int64}}:
 [4, 5, 6, 31, 46, 53]
 [7, 17, 48]
 [3, 16, 26, 39, 41, 51, 52]
 [1, 10, 12, 13, 19, 21, 43, 54]
 []
 [9, 28, 32, 37, 42]
 [15, 23, 27, 30, 34, 40]
 [2, 11, 20, 22, 38, 44, 47]
 [8, 14, 24, 33, 36, 45, 50, 55]
 [18, 25, 29, 35, 49]

```

If you want none of them to be empty, one way would be to assign the first `k` elements of `x` deterministically for instance.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 31, 2024, 1:34pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/7 "2024-01-31T13:34:05Z")

</div>

Thanks a lot!!!

---

<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 31, 2024, 2:38pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/8 "2024-01-31T14:38:42Z")

</div>

Using the prototypical decomposition of 55 = 1+2+…+10, it seems you might be looking for something like:

```julia
julia> using Random

julia> shuffle(getindex.(Ref(shuffle(1:55)),(:).(cumsum(0:9).+1,cumsum(1:10))))
10-element Vector{Vector{Int64}}:
 [29, 52, 30]
 [5, 24, 28, 21, 27, 22]
 [12, 38, 31, 26, 16, 23, 9, 36, 18]
 [13, 45, 46, 34, 14, 41, 19, 10, 3, 4]
 [8, 37, 7, 11]
 [51, 6]
 [54, 15, 40, 48, 20, 35, 50]
 [17, 53, 1, 2, 44]
 [39]
 [42, 43, 49, 32, 33, 25, 55, 47]

```

There are a few optimizations still possible on this expression, but is this in the direction you are looking?

---

<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 31, 2024, 4:18pm UTC](https://discourse.julialang.org/t/partitioning-a-vector/109511/9 "2024-01-31T16:18:43Z")

</div>

Perhaps a [similar discussion](https://discourse.julialang.org/t/split-vector-into-n-potentially-unequal-length-subvectors/73548) might be of interest
