


RANDISCR Generate discrete random numbers with specified probabiities [X]=(P,N,A)
Inputs: P vector of probabilities (not necessarily normalized)
N number of random values to generate [default = 1]
A output alphabet [default = 1:length(p)]
Outputs: X vector of 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.

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