Home > voicebox > randiscr.m

randiscr

PURPOSE ^

RANDISCR Generate discrete random numbers with specified probabiities [X]=(P,N,A)

SYNOPSIS ^

function x=randiscr(p,n,a)

DESCRIPTION ^

RANDISCR Generate discrete random numbers with specified probabiities [X]=(P,N,A)

 Usage: (1) randiscr([],10)        % generate 10 uniform random binary values
        (2) randiscr(2:6,10)       % generate 10 random numbers in the range 1:5
                                     with probabilities [2 3 4 5 6]/20
        (3) randiscr([],10,'abcd') % generate a string of 10 random
                                     characters equiprobable from 'abcd'

 Inputs: P  vector of probabilities (not necessarily normalized) [default = uniform]
         N  number of random values to generate [default = 1]
         A  output alphabet [default = 1:length(p) or 0:1 if p is empty]

 Outputs: X  vector of not necessarily distinct values taken from alphabet A

 The vector P is internally normalized by dividing by its sum.
 If P is an M-dimensional matrix (and A is unspecified), then X will
 have dimensions (N,M) with the corresponding indices for each dimension.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function x=randiscr(p,n,a)
0002 %RANDISCR Generate discrete random numbers with specified probabiities [X]=(P,N,A)
0003 %
0004 % Usage: (1) randiscr([],10)        % generate 10 uniform random binary values
0005 %        (2) randiscr(2:6,10)       % generate 10 random numbers in the range 1:5
0006 %                                     with probabilities [2 3 4 5 6]/20
0007 %        (3) randiscr([],10,'abcd') % generate a string of 10 random
0008 %                                     characters equiprobable from 'abcd'
0009 %
0010 % Inputs: P  vector of probabilities (not necessarily normalized) [default = uniform]
0011 %         N  number of random values to generate [default = 1]
0012 %         A  output alphabet [default = 1:length(p) or 0:1 if p is empty]
0013 %
0014 % Outputs: X  vector of not necessarily distinct values taken from alphabet A
0015 %
0016 % The vector P is internally normalized by dividing by its sum.
0017 % If P is an M-dimensional matrix (and A is unspecified), then X will
0018 % have dimensions (N,M) with the corresponding indices for each dimension.
0019 
0020 % Somewhat similar in function to RANDSRC in the comms toolbox
0021 
0022 %   Copyright (c) 2005-2012 Mike Brookes,  mike.brookes@ic.ac.uk
0023 %      Version: $Id: randiscr.m 2189 2012-07-20 13:47:00Z dmb $
0024 %
0025 %   VOICEBOX is a MATLAB toolbox for speech processing.
0026 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 %   This program is free software; you can redistribute it and/or modify
0030 %   it under the terms of the GNU General Public License as published by
0031 %   the Free Software Foundation; either version 2 of the License, or
0032 %   (at your option) any later version.
0033 %
0034 %   This program is distributed in the hope that it will be useful,
0035 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0036 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0037 %   GNU General Public License for more details.
0038 %
0039 %   You can obtain a copy of the GNU General Public License from
0040 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0041 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0043 gota=nargin>2;
0044 if nargin<1 || ~numel(p)
0045     if gota
0046         p=ones(1,length(a));
0047     else
0048         p=ones(1,2);
0049         a=(0:1)';
0050         gota=1;
0051     end
0052 end
0053 if nargin<2 || ~numel(n)
0054     n=1;
0055 end
0056 d=length(p(:)); % size of output alphabet
0057 z=zeros(d+n-1,1); % array to hold random numbers
0058 z(1:d)=cumsum(p(:)/sum(p(:))); % last value is actually overwritten in the next line
0059 z(d:d+n-1)=rand(n,1);
0060 [y,iy]=sort(z);
0061 y(iy)=(1:d+n-1)';
0062 m=zeros(d+n-1,1);
0063 m(y(1:d-1))=1;
0064 m(1)=m(1)+1;
0065 mc=cumsum(m);
0066 x=mc(y(d:d+n-1));
0067 if gota
0068     x=a(x);
0069 elseif length(p(:))>length(p) % need multiple dimensions
0070     v=x-1;
0071     s=cumprod(size(p));
0072     m=length(s);
0073     s(2:end)=s(1:end-1);
0074     s(1)=1;
0075     x=zeros(n,m);
0076     for i=m:-1:1
0077         x(:,i)=1+floor(v/s(i));
0078         v=rem(v,s(i));
0079     end
0080 end

Generated on Fri 22-Sep-2017 19:37:38 by m2html © 2003