Any ideas to generate all possible subarrays from an array?

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

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

julia> [1, 2];

julia> [1, 3];

julia> [2, 3];

julia> [1];

julia> [2];

julia> [3];
``````

Thank you!

``````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]
``````
1 Like

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 …?)

1 Like

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” …

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”.

2 Likes

That clarifies my confusion.

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.

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.