Create all binary valued vectors of specific length
How do I efficiently create a matrix of shape (2**N, N) with all binary vectors of length N as its rows? I call a vector binary iff each of its components equals either 0 or 1. I want the matrix to have dtype=torch.float.

Seb's answer but modified for pytorch:

def gen_all_binary_vectors(length: int) -&gt; torch.Tensor:
    return ((torch.arange(2**length).unsqueeze(1) &gt;&gt; torch.arange(length-1, -1, -1)) &amp; 1).float()

