File Exchange

image thumbnail

enumBase2

version 1.0.0.0 (338 Bytes) by Shawn Mikula
Constructs a character or numeric array that enumerates from 0 to (2^n - 1) in binary.

0 Downloads

Updated 21 Aug 2006

No License

a = enumBase2(n) constructs a cell array that enumerates from 0 to (2^n - 1) in binary.

a = enumBase2(n,outputformat) constructs an array that enumerates from 0
to (2^n - 1) in binary. The type of array output is given by
outputformat. By default, outputformat=1, which outputs a cell array.
If outputformat=2, then the output is a numeric array.

Examples
-------
numBase2(4) returns the following cell array:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

numBase2(4,2) output the same as above, except as a numeric array.

Useful for combinatorics.

Cite As

Shawn Mikula (2020). enumBase2 (https://www.mathworks.com/matlabcentral/fileexchange/12007-enumbase2), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

John Lei

just what I was looking for

Duane Hanselman

Please don't use user reviews as a way to distribute code. User reviews cannot be deleted, modified or updated. Your code belongs under the Download Now button, not here!

Shawn Mikula

Didn't realize my comment would be posted immediately. Should have said that the updated enumBase2 should appear in 1-2 days. But for those who don't want to wait, here it is:

function [a] = enumBase2(varargin)

% a = enumBase2(n) constructs a cell array that enumerates from 0 to (2^n - 1) in binary.
%
% a = enumBase2(n,outputformat) constructs an array that enumerates from 0
% to (2^n - 1) in binary. The type of array output is given by
% outputformat. By default, outputformat=1, which outputs a cell array.
% If outputformat=2, then the output is a numeric array.
%
% Examples
% -------
% numBase2(4) returns the following cell array:
% 0000
% 0001
% 0010
% 0011
% 0100
% 0101
% 0110
% 0111
% 1000
% 1001
% 1010
% 1011
% 1100
% 1101
% 1110
% 1111
%
% numBase2(4,2) output the same as above, except as a numeric array.
%
% Useful for combinatorics.

try
n = varargin{1}
catch
error('try "help enumBase2"')
end
try
formout = varargin{2}
catch
formout = 1;
end
if formout==1
a=dec2bin(0:(2^n-1));
elseif formout==2
aTemp=dec2bin(0:(2^n-1));
a=ones(2^n-1,n);
for i=1:n
for j=1:2^n
a(j,i)=str2num(aTemp(j,i));
end
end
else
error('try "help enumBase2"')

end

Shawn Mikula

thanks John! I have incorporated your suggestion to enhance performance and have added the option to output either a numeric array or a cell array.

John D'Errico

A minor point, but this should be just a wrapper for the one line idiom:

dec2bin(0:(2^n-1))

Except that this code uses a loop, and fails to preallocate its array, so it will be REALLY, REALLY slow for even moderately large n. Note the examples below as n grows from 5 to 15.

>> n=5;
>> tic,c = dec2bin(0:(2^n-1));toc
Elapsed time is 0.000500 seconds.
>> tic,c=enumBase2(n);toc
Elapsed time is 0.002035 seconds.

>> n=10;
>> tic,c = dec2bin(0:(2^n-1));toc
Elapsed time is 0.005724 seconds.
>> tic,c=enumBase2(n);toc
Elapsed time is 0.131692 seconds.

>> n=15;
>> tic,c = dec2bin(0:(2^n-1));toc,tic,c=enumBase2(n);toc
Elapsed time is 0.269849 seconds.
>> tic,c=enumBase2(n);toc
Elapsed time is 123.935903 seconds.

If you want a code that does more than the dec2bin idiom, but is actually efficient, take a look at allcomb [FEX:10054].

Updates

1.0.0.0

update to include additional functionality and incorporate user comment to improve performance

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux