MATLAB: How to create a 2^n by n matrix that the rows of the matrix enumerate all the possibilities for a n digit binary number

algorithm

For example, for n=3, there are 8 rows, and they are 000,100,010,001,110,101,001,111 (may not in this order). Any fast way to realize it using a function, say f(n)?

Best Answer

Use dec2bin to get the string representation. Subtract '0' from the string to convert characters '0' and '1' to numbers 0 and 1:
n = 3;
dec2bin(0:pow2(n)-1) - '0'