Search code examples
scilabhypercube

What is a way to create all vectors from the hypercube in dimension n?


I want to store in a matrix all the vectors that are created by the hypercube in dimension n, that is, for dimension n=3 for example :

matrix = [[0 0 0], [0 0 1], [0 1 0], [0 1 1],
          [1 0 0], [1 0 1], [1 1 0], [1 1 1]]

Is there a good way to do it in scilab ?

Thank you !


Solution

  • Maybe this way (this is just like counting from 0 to 2^n-1 in binary)

    n=3;
    mat = zeros(2^n,n);
    for i = 1:2^n
        mat(i,:) = bitget(i-1,1:n); 
    end
    
    --> mat
     mat  = 
    
       0.   0.   0.
       1.   0.   0.
       0.   1.   0.
       1.   1.   0.
       0.   0.   1.
       1.   0.   1.
       0.   1.   1.
       1.   1.   1.