Home > chronux > test > myrandint.m

myrandint

PURPOSE ^

MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries

SYNOPSIS ^

function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)

DESCRIPTION ^

 MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries 
 drawn with replacement from elements of vector RANGE.  The elements in
 vector RANGE do not need to be contiguous or unique.  (Actually, they do
 not even need to be integers: The function works the exact same way with
 noninteger elements, but a warning is generated to alert the user that 
 noninteger elements are being sampled.) 

 To specify a contiguous integer range from Xlow to Xhi, use RANGE = [Xlow:Xhi].  
 
 MYRANDINT(M,N,RANGE,'noreplace') is an M-by-N matrix with random integers 
 drawn without replacement.

 This function is based around RAND and RANDPERM, and is intended as a
 modest imitation of Comm Toolbox's RANDINT.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)
0002 % MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries
0003 % drawn with replacement from elements of vector RANGE.  The elements in
0004 % vector RANGE do not need to be contiguous or unique.  (Actually, they do
0005 % not even need to be integers: The function works the exact same way with
0006 % noninteger elements, but a warning is generated to alert the user that
0007 % noninteger elements are being sampled.)
0008 %
0009 % To specify a contiguous integer range from Xlow to Xhi, use RANGE = [Xlow:Xhi].
0010 %
0011 % MYRANDINT(M,N,RANGE,'noreplace') is an M-by-N matrix with random integers
0012 % drawn without replacement.
0013 %
0014 % This function is based around RAND and RANDPERM, and is intended as a
0015 % modest imitation of Comm Toolbox's RANDINT.
0016 
0017 
0018 if isequal(size(outputRange),[1 2]) && ~isequal(outputRange(1),outputRange(2)-1),
0019     warning('To specify a range [low high] use [low:high].')
0020 end
0021 if ~isequal(round(outputRange),outputRange),
0022     warning('Specified RANGE contains noninteger values.')
0023 end
0024 if ~isequal(length(outputRange),length(outputRange(:))),
0025     error('Range must be a vector of integer values.')
0026 end
0027 
0028 numElements = outputRow*outputCol;
0029 
0030 if isempty(varargin),
0031     
0032     ranInt = zeros(outputRow,outputCol);
0033     randIx = floor((length(outputRange))*rand(size(ranInt))) + 1;
0034     ranInt = outputRange(randIx);
0035     if ~isequal(size(randIx),size(ranInt)),
0036         ranInt = reshape(ranInt,size(randIx));
0037     end
0038     
0039 elseif isequal(varargin{1},'noreplace'),
0040     
0041     if numElements > length(outputRange),
0042         error('Not enough elements in range to sample without replacement.')
0043     else
0044         % Generate full range of integers
0045         XfullShuffle = outputRange(randperm(length(outputRange)));
0046         % Select the first bunch:
0047         ranInt = reshape(XfullShuffle(1:numElements),outputRow,outputCol);     
0048     end    
0049     
0050 else
0051     error('Valid argument is ''noreplace''.')
0052 end
0053 
0054

Generated on Fri 28-Sep-2012 12:34:30 by m2html © 2005