|
On Mar 19, 3:06 pm, "Vinh Le" <lekhanhv...@gmail.com> wrote:
> I would like to write a small script to generate a series of array to have all possible combination of binary number.
>
> Here is an example
> n = 2, the result will be
> [0,0]
> [0,1]
> [1,0]
> [1,1]
>
> n = 3
> [0,0,0], the result will be
> [0.0.1]
> ....
> Any help would be appreciated
A short for loop can do this.
%%%%%%%%%%%%%%%%%%%%%%%%%
function r = permutebin(n)
rows = 2^n;
cols = log2(rows);
r = zeros(rows,cols);
for i=1:rows
r(i,:) = dec2bin(i-1,cols);
end
r = char(r);
%%%%%%%%%%%%%%%%%%%%%%%%%
call as:
r = permutebin(n)
ex:
r = permutebin(4)
%%%%%%%%%%%%%%%5
r =
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
-Nathan
|