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

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

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!

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

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

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?

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.

1 Like

Broadcast digits:

digits.(0:(2^N-1), base=2, pad=N)
3 Likes

See this other post for an alternative solution using Base Iterators.product:

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

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.

3 Likes

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