# Is there a way to list out all possible permutations of a sequence of indicator functions?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 9, 2022, 12:49pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532 "2022-08-09T12:49:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [August 9, 2022, 12:49pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/1 "2022-08-09T12:49:01Z")

</div>

Suppose I want to list out all possible permutations of N indicator functions. e.g. if N = 2 then I would want to return `(1,0),(0,1),(1,1),(0,0)` Is there a function to do this in Julia? I know I can collect permutations with the combinatorics package but that seems to require a fixed array. So if I did

```julia
collect(permutations(0:1,2))
2-element Vector{Vector{Int64}}:
[0, 1]
[1, 0]

```

Whereas if the number of 1’s is Bernoulli then the array to be permutated will vary. So just wondering if there was a built in function that I am missing. Thanks!

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [August 9, 2022, 12:54pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/2 "2022-08-09T12:54:49Z")

</div>

Not sure what you actually want to achieve in general and what you mean indicator functions but maybe the following might help

```julia
N=2
for i=0:2^N-1
    @show digits(i,base=2,pad=N)
end

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [August 9, 2022, 1:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/3 "2022-08-09T13:17:13Z")

</div>

Awesome thanks so much! sorry I was unclear. I just mean that if I have N coin tosses and I want to list out all possible outcomes if there was a built in function to do that? So if N = 6 I should have 64 permutations I think. But with the included code I only get thirty some results? On the other hand if I try `i=0:2^N` I get a vector with 7 entries? Also would it be possible to store the output as an array with @show?

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [August 9, 2022, 1:21pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/4 "2022-08-09T13:21:35Z")

</div>

The code I provided does that i think. For N=6 I get indeed 64 different outcomes  
(note `2^N-1` is `(2^N) -1` )  
As for storing, up to you to store them in anything you want. You have `i `and the array of digits, so you can store into an array of array, or a matrix or whatever you need. I just wrote the @show to show what you get with the digits approach.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 9, 2022, 1:40pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/5 "2022-08-09T13:40:09Z")

</div>

> [@phantom](#):
>
> would it be possible to store the output as an array

Broadcast `digits`:

```julia
digits.(0:(2^N-1), base=2, pad=N)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 9, 2022, 3:16pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/6 "2022-08-09T15:16:03Z")

</div>

See [this other post](https://discourse.julialang.org/t/iterator-over-variable-number-of-arguments/3558/3) for an alternative solution using Base Iterators.product:

```julia
import Base.Iterators: flatten, product
N = 6
collect(flatten([product([0:1 for _ in 1:N]...)])) 

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 9, 2022, 3:41pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/7 "2022-08-09T15:41:10Z")

</div>

Note that storing them as arrays is wildly inefficient. If you want an efficient representation of an indicator function, you cannot do better than a binary integer.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [August 9, 2022, 5:37pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-list-out-all-possible-permutations-of-a-sequence-of-indicator-functions/85532/8 "2022-08-09T17:37:43Z")

</div>

awesome thanks, yes I mistook the original for 2^(N-1). Thanks to everyone for all the very helpful comments and making Julia accessible!
