# Any ideas to generate all possible subarrays from an array?

**URL:** <https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285>\
**Category:** New to Julia\
**Tags:** arrays\
**Created:** [January 9, 2022, 2:40pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285 "2022-01-09T14:40:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [January 9, 2022, 2:40pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/1 "2022-01-09T14:40:22Z")

</div>

RT. There is an array, like [1, 2, 3]. What’s a proper way to have all subarrays from the array as below?

```julia
julia> [1, 2, 3];

julia> [1, 2];

julia> [1, 3];

julia> [2, 3];

julia> [1];

julia> [2];

julia> [3];

```

Thank you!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 9, 2022, 2:45pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/2 "2022-01-09T14:45:58Z")

</div>

```julia
julia> using Combinatorics

julia> collect(combinations([1,2,3]))
7-element Vector{Vector{Int64}}:
 [1]
 [2]
 [3]
 [1, 2]
 [1, 3]
 [2, 3]
 [1, 2, 3]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 9, 2022, 2:52pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/3 "2022-01-09T14:52:31Z")

</div>

> [@oheil](#):
>
> `collect(combinations([1,2,3]))`

This may or may not be what the original poster wants. It returns all possible _subsets_ of the array. But it’s not clear if that’s what @Xiao means by “subarray”. (e.g. do you only want _consecutive_ subarrays, or …?)

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [January 9, 2022, 2:57pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/4 "2022-01-09T14:57:16Z")

</div>

Thanks @stevengj ! That’s what I want. I should use " subsets of the array" in my question, because there’re already something like “Julia’s SubArray” …

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 9, 2022, 3:16pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/5 "2022-01-09T15:16:02Z")

</div>

> [@stevengj](#):
>
> This may or may not be what the original poster wants. It returns all possible _subsets_ of the array. But it’s not clear if that’s what @Xiao means by “subarray”. (e.g. do you only want _consecutive_ subarrays, or …?)

How is this remark helpful? Is it meant for me (should I improve my answering techniques?) or is it meant for OP to clarify the question?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 9, 2022, 3:37pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/6 "2022-01-09T15:37:29Z")

</div>

> [@oheil](#):
>
> How is this remark helpful? Is it meant for me (should I improve my answering techniques?) or is it meant for OP to clarify the question?

It’s meant for the OP to clarify the question. In particular, the `[1,2,3]` example is too small to distinguish between different colloquial meanings of “subarray”.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 9, 2022, 3:38pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/7 "2022-01-09T15:38:31Z")

</div>

> [@stevengj](#):
>
> It’s meant for the OP to clarify the question.

That clarifies my confusion.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 9, 2022, 3:42pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/8 "2022-01-09T15:42:34Z")

</div>

It is not that small. The first example does not have `[1, 3]`, so I would assume consecutive. Alas, the post marked as a solution has `[1, 3]` in the output, so I am not sure what OP really wants.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 9, 2022, 3:52pm UTC](https://discourse.julialang.org/t/any-ideas-to-generate-all-possible-subarrays-from-an-array/74285/9 "2022-01-09T15:52:57Z")

</div>

> [@Henrique\_Becker](#):
>
> so I am not sure what OP really wants.

Perhaps it is enough to point to package Combinatorics.

In general I would suppose that you stop talking with others about OPs intentions. Ask him directly. For me as non native english this sounds a bit weird and creates some confusion for me. Just a friendly (with quite some uncertainty) meant remark.
