No BSD License  

Highlights from
enumBase2

3.0

3.0 | 2 ratings Rate this file 3 Downloads (last 30 days) File Size: 373 Bytes File ID: #12007

enumBase2

by Shawn Mikula

 

19 Aug 2006 (Updated 21 Aug 2006)

Constructs a character or numeric array that enumerates from 0 to (2^n - 1) in binary.

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
21 Aug 2006 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].

21 Aug 2006 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.

21 Aug 2006 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

21 Aug 2006 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!

21 Aug 2006 John Lei

just what I was looking for

Please login to add a comment or rating.
Updates
21 Aug 2006

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

Tag Activity for this File
Tag Applied By Date/Time
statistics Shawn Mikula 22 Oct 2008 08:35:20
probability Shawn Mikula 22 Oct 2008 08:35:20
combinatorics Shawn Mikula 22 Oct 2008 08:35:20
permutations Shawn Mikula 22 Oct 2008 08:35:20
binary Shawn Mikula 22 Oct 2008 08:35:20
statistics and probability Shawn Mikula 22 Oct 2008 08:35:20

Contact us at files@mathworks.com