Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all possible permutations in julia

Tags:

binary

julia

What I am trying to do is generate all possible permutations of 1 and 0 given a particular sample size. For instance with a sample of n=8 I would like the m = 2^8 = 256 possible permutations, i.e:

enter image description here

I have been doing this in R, but it is very slow. Is there a quick way to do this in the Julia programming language?

like image 672
Alejandro Ochoa Avatar asked Mar 15 '23 03:03

Alejandro Ochoa


1 Answers

These are just the numbers from 0 to 2^k-1, written in binary.

# Strings
k=8
[ bin(n,k) for n in 0:2^k-1 ]

# Arrays
[ [ bit == '1' ? 1 : 0 for bit in bin(n,k) ] for n in 0:2^k-1 ]
like image 117
Vincent Zoonekynd Avatar answered Mar 30 '23 13:03

Vincent Zoonekynd